-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLeetCode#120.cc
45 lines (40 loc) · 1.28 KB
/
LeetCode#120.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
private:
bool binary(int A[],int l, int r, int target){
int low = l, high = r;
while(low <= high){
int mid = (low+high)/2;
if(A[mid]==target) return true;
else if(target < A[mid]) high = mid-1;
else low = mid+1;
}
return false;
}
public:
bool search(int A[], int n, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n==0) return false;
if(n==1) return A[0]==target;
int l,r;
if(A[0]<A[n-1]) return binary(A,0,n-1,target);
else if(A[0]>A[n-1]){l=0;r=n-1;}
else{
if(A[0]==target) return true;
l = 1;
while(l<n && A[l]==A[l-1]) l++;
if(l==n) return A[0]==target;
if(A[l]<A[0]) return binary(A,l,n-1,target);
r=n-2;
while(r>=0&&A[r]==A[r+1]) r--;
if(A[r] > A[n-1]) return binary(A,l,r,target);
}
int low = l, high = r;
while(low<=high){
int mid = (low+high)/2;
if(A[mid]>=A[l]) low=mid+1;
else high = mid-1;
}
return binary(A,l,high,target) || binary(A,high+1,r,target);
}
};