Created
December 17, 2018 03:31
-
-
Save zhangxiaomu01/3fe14ccf02cb740d9765be63f91a02d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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