您的位置:首页 > 其它

[LeetCode]81 Search in Rotated Sorted Array II

2015-01-02 16:47 555 查看
https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/
http://fisherlei.blogspot.com/2013/01/leetcode-search-in-rotated-sorted-array_3.html
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