题目链接:search-in-rotated-sorted-array-ii
/**
*
* Follow up for "Search in Rotated Sorted Array":
* What if duplicates are allowed?
*
* Would this affect the run-time complexity? How and why?
*
* Write a function to determine if a given target is in the array.
*
*/
public class SearchInRotatedSortedArrayII {
// 271 / 271 test cases passed.
// Status: Accepted
// Runtime: 259 ms
// Submitted: 1 minute ago
public boolean search(int[] A, int target) {
if(A.length == 0) return false;
int low = 0; int high = A.length;
while(low != high) {
int mid = low + (high - low) / 2;
if(A[mid] == target) return true;
if(A[low] < A[mid]) {
if(A[low] <= target && target < A[mid]) high = mid;
else low = mid + 1;
} else if(A[low] > A[mid]){
if(target <= A[high - 1] && A[mid] < target) low = mid + 1;
else high = mid;
} else {
low ++;
}
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}