一、题目
二、思路
三、代码
class Solution {
public:
int search(vector<int>& arr, int target) {
int size=arr.size();
int left=0,right=size-1;
int mid=0;
while(left<=right){
mid=(left+right)/2;
//左边left相等直接返回
if(arr[left]==target){
return left;
}
//中间相等
if(arr[mid]==target){
right=mid;
}
//大于左边
else if(arr[mid]>arr[left]){
if(target>=arr[left] && target<arr[mid]){
right=mid-1;
}
else{
left=mid+1;
}
}
//小于左边
else if(arr[mid]<arr[left]){
if(target>arr[mid] && target<=arr[right]){
left=mid+1;
}
else{
right=mid-1;
}
}
//相等
else{
left++;
}
}
return -1;
}
};