• 微信公众号：美女很有趣。 工作之余，放松一下，关注即送10G+美女照片！

# 81. Search in Rotated Sorted Array II

2周前 (04-07) 9次浏览

``````int l = 0, r = n - 1;
while (l < r) {
int mid = l + r + 1 >> 1;
if (nums[mid] >= nums[0]) {
l = mid;
} else {
r = mid - 1;
}
}
``````

``````while(left<right&&nums[0]==nums[right])right--;
``````

``````class Solution {
public:
bool search(vector<int>& nums, int target) {
int n=nums.size();
if(n==0) return false;
if(n==1) return nums[0]==target;
int left=0,right=n-1;
int end=n;
while(left<right&&nums[0]==nums[right])right--;
while(left<right){
int mid =(left+right+1)>>1;
if(nums[mid]>=nums[0]) {
left=mid;
}
else{
right=mid-1;
}
}
if(nums[right]>=nums[0]&&right+1<n) end=right+1;
int res = binary_search(nums,target,0,end-1);
if(res!=-1) return true;
res = binary_search(nums,target,end,n-1);
return res!=-1;
}
int binary_search(vector<int>& nums,int target,int start,int end){
int left=start,right=end;
while(left<right){
int mid=(left+right)/2;
if(nums[mid]>=target) right=mid;
else  left=mid+1;
}
return nums[right]==target?right:-1;
}
};
``````