-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path081.cpp
31 lines (31 loc) · 887 Bytes
/
081.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
class Solution {
public:
bool search(vector<int>& nums, int target) {
int len = nums.size();
if(len == 0) return false;
int l = 0, r = len - 1, m;
while(l <= r)
{
m = (l + r) >> 1;
if(nums[m] == target)
return true;
while(nums[m] == nums[l] && nums[m] == nums[r])
{
l++;
r--;
}
// 二分法,每一个区间都可能是一个断崖式的排序,需要比较四种情况
if(nums[m] >= nums[l])
{
if(target >= nums[l] && target < nums[m]) r = m - 1;
else l = m + 1;
}
else
{
if(target <= nums[r] && target > nums[m]) l = m + 1;
else r = m - 1;
}
}
return false;
}
};