class Solution {
public:
bool check(vector<int> &nums,int target,int l,int r)//[l,r]区间查找target
{
while(l<r)
{
int mid=(l+r+1)>>1;
if(target>=nums[mid]) l=mid;
else r=mid-1;
}
return nums[l]==target;
}
bool search(vector<int>& nums, int target) {
int n=nums.size()-1;
while(n&&nums[n]==nums[0]) n--;
if(nums[n]>nums[0]) //说明只有一段单调的
return check(nums,target,0,n);
int l=0,r=n;
while(l<r)
{
int mid=(l+r)>>1;
if(nums[mid]>=nums[0]) l=mid+1;
else r=mid;
}
//根据target与nums[0]的关系,判断二分区间
if(target>=nums[0]) return check(nums,target,0,l-1);
else return check(nums,target,l,n);
}
};
标签:return,target,nums,int,mid,Leetcode81,II,排序,check
From: https://www.cnblogs.com/tangxibomb/p/17266638.html