-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.cpp
43 lines (33 loc) · 1.1 KB
/
search.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
41
42
43
class Solution {
public:
bool search(vector<int>& nums, int target) {
int n = nums.size();
if(n == 0) return false;
int low = 0, high = n - 1, mid, first = nums[0];
while(low <= high) {
while(low < high && nums[low] == nums[low + 1])
low++;
while(low < high && nums[high] == nums[high - 1])
high--;
mid = low + (high - low) / 2;
int value = nums[mid];
if(value == target) return true;
bool am_big = value >= first;
bool target_big = target >= first;
if(am_big == target_big) {
if(value < target) {
low = mid + 1;
} else {
high = mid - 1;
}
} else {
if(am_big) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
return false;
}
};