-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path81.txt
37 lines (34 loc) · 1.21 KB
/
81.txt
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
// class Solution {
// public:
// bool search(vector<int>& nums, int target) {
// }
// };
class Solution {
public:
bool search(vector<int>& nums, int target) {
int left=0,right=nums.size()-1,mid;
while(left<=right){
mid=left+(right-left)/2;
if(nums[mid] == target) return true;
else if(nums[mid] == nums[left]) left++;
else if(nums[mid] == nums[right]) right--;
else if(nums[0]>target){ //目标在右子数组
if(nums[mid]>=nums[0]) left=mid+1; //中点在左子数组
else{ //中点在右子数组
if(nums[mid]==target) return true;
else if(nums[mid]<target) left=mid+1;
else right=mid-1;
}
}
else {//目标在左子数组
if(nums[mid] <= nums[0]) right=mid-1;//中点在右子数组
else{ //中点在左子数组
if(nums[mid]==target) return true;
else if(nums[mid]<target) left=mid+1;
else right=mid-1;
}
}
}
return false;
}
};