class Solution {
public:
bool search(vector<int>& nums, int target) {
int n=nums.size();
int l=0;
int r=n-1;
while(l<=r)
{
int mid=(l+r)/2;
if(nums[mid] == target) return true;
if(nums[mid] == nums[l])
{
l++;
continue;
}
if(nums[mid] > nums[l])
{
if(nums[mid] > target && nums[l] <= target) r=mid-1;
else l=mid+1;
}
else
{
if(nums[mid] < target && nums[r] >= target) l=mid+1;
else r=mid-1;
}
}
return false;
}
};
无重复
class Solution {
public:
int search(vector<int>& nums, int target) {
int n=nums.size();
int l=0;
int r=n-1;
while(l<=r)
{
int mid=(l+r)/2;
if(nums[mid] == target) return mid;
if(nums[mid] >= nums[l])//注意这里是大于等于
{
if(nums[mid] > target && nums[l] <= target) r=mid-1;
else l=mid+1;
}
else
{
if(nums[mid] < target && nums[r] >= target) l=mid+1;
else r=mid-1;
}
}
return -1;
}
};