-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path81.cpp
More file actions
31 lines (31 loc) · 906 Bytes
/
81.cpp
File metadata and controls
31 lines (31 loc) · 906 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
29
30
31
class Solution {
public:
bool search(vector<int>& nums, int target) {
int n = nums.size();
if(n==0)
return false;
if(n==1)
return target==nums[0];
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target)
return true;
else if (nums[mid] < nums[right]) {
if (nums[mid] < target && target <= nums[right])
left = mid + 1;
else
right = mid - 1;
}
else if (nums[mid] > nums[right]) {
if (nums[left] <= target && target < nums[mid])
right = mid - 1;
else
left = mid + 1;
}
else
right--;
}
return false;
}
};