标签:leetcode
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
标签:leetcode
原文地址:http://7371901.blog.51cto.com/7361901/1599018