首页 > 代码库 > [LeetCode]81 Search in Rotated Sorted Array II
[LeetCode]81 Search in Rotated Sorted Array II
https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/
http://blog.csdn.net/linhuanmars/article/details/20588511
public class Solution { public boolean search(int[] A, int target) { if (A == null || A.length == 0) return false; return find(A, 0, A.length - 1, target); } // O(n) private boolean find(int[] A, int low, int high, int t) { if (low > high) return false; if (low == high) return A[low] == t; int mid = (low + high) / 2; if (A[mid] == t) return true; if (A[mid] > A[low]) { if (A[low] <= t && t < A[mid]) return find(A, low, mid - 1, t); // Left part else return find(A, mid + 1, high, t); } else if (A[mid] < A[low]) { if (t > A[mid] && t <= A[high]) return find(A, mid + 1, high, t); // Right part else return find(A, low, mid - 1, t); } else { return find(A, low + 1, high, t); // Just shift left. } } }
[LeetCode]81 Search in Rotated Sorted Array II
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。