题目大意:旋转数组中寻找给定的值
解题思路:二分查找。
class Solution {
public:
bool search(int A[], int n, int target) {
return search(A, 0, n - 1, target);
}
private:
bool search(int A[], int low, int high, int target) {
while(low <= high) {
int mid = (low + high) / 2;
if(A[mid] == target) {
return true;
}
if(A[mid] < A[high]) {
if(A[mid] < target && target <= A[high]) {
low = mid + 1;
} else {
high = mid - 1;
}
} else if(A[mid] > A[high]) {
if(A[mid] > target && target >= A[low]) {
high = mid - 1;
} else {
low = mid + 1;
}
} else {
if(search(A, mid + 1, high, target)) {
return true;
}
if(search(A, low, mid - 1, target)) {
return true;
}
return false;
}
}
return false;
}
};