一、题目
二、思路
三、代码
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;
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;
}
};
|