forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0081.cpp
More file actions
28 lines (26 loc) · 741 Bytes
/
0081.cpp
File metadata and controls
28 lines (26 loc) · 741 Bytes
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
class Solution {
public:
bool search(vector<int>& nums, int target) {
int l = 0;
int r = nums.size() - 1;
while (l <= r) {
int m = (l + r) >> 1;
if (nums[m] == target) return true;
if (nums[l] == nums[m] && nums[r] == nums[m]) {
l++;
r--;
} else if (nums[l] <= nums[m]) {
if (nums[l] <= target && target < nums[m])
r = m - 1;
else
l = m + 1;
} else {
if (nums[m] < target && target <= nums[r])
l = m + 1;
else
r = m - 1;
}
}
return false;
}
};