-
Notifications
You must be signed in to change notification settings - Fork 79
/
solution.cpp
40 lines (38 loc) · 983 Bytes
/
solution.cpp
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
class Solution
{
public:
bool search(int A[], int n, int target)
{
if(NULL == A || 0 == n)
return false;
int left = 0, right = n-1, mid = 0;
while(left <= right)
{
mid = left+(right-left)/2;
if(A[mid] == target)
return true;
bool isLeft = true;
for(int i = left; i <= mid; ++i)
if(A[i] > A[mid])
{
isLeft = false;
break;
}
if(isLeft)//left
{
if(A[left] <= target && target < A[mid])
right = mid - 1;
else
left = mid + 1;
}
else//right
{
if(A[mid] < target && target <= A[right])
left = mid + 1;
else
right = mid - 1;
}
}
return false;
}
};