Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created December 17, 2018 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/3fe14ccf02cb740d9765be63f91a02d2 to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/3fe14ccf02cb740d9765be63f91a02d2 to your computer and use it in GitHub Desktop.
class Solution {
public:
int search(vector<int>& nums, int target) {
int len_n = nums.size();
int lo = 0, hi = len_n-1;
int mid, s_ind;
while(lo<hi)
{
mid = (lo+hi)/2;
if(nums[mid]>nums[hi]){
lo = mid+1;
}
else
hi = mid;
}
s_ind = lo;
lo = 0, hi = len_n - 1;
while(lo<=hi){
mid = (lo + hi)/2;
int realmid = (mid + s_ind)%len_n;
if(nums[realmid] == target)
return realmid;
if(nums[realmid]>target){
hi = mid-1;
}
else if(nums[realmid]<target)
lo = mid+1;
}
return -1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment