题目
c++
二分
class Solution {
public:
int search(vector<int>& nums, int target) {
if(nums.size()==0)
return -1;
int start=0;
int end = nums.size()-1;
int ans=-1;
while(start<end)
{
int mid = (start+end)/2;
if(target < nums[mid])
{
if(target>=nums[start]&&nums[start]<nums[mid])
{
end = mid-1;
}
else if(target<=nums[end]&&nums[end]<nums[mid])
{
start = mid+1;
}
else
end=mid-1;
}
else if(target > nums[mid])
{
if(target>=nums[start]&&nums[start]>nums[mid])
{
end = mid-1;
}
else if(target>=nums[end]&&nums[end]<nums[mid])
{
start = mid+1;
}
else
start = mid+1;
}
else
{
ans=mid;
break;
}
}
if(ans!=-1)
return ans;
ans = start;
if(nums[start]!=target)
ans=-1;
return ans;
}
};