Created
December 18, 2018 01:16
-
-
Save zhangxiaomu01/7cb2f16c3f9faf44d9ea778fb4c68e18 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 lo = 0, hi = nums.size() - 1; | |
while(lo<=hi){ | |
int mid = (lo + hi)/2; | |
if(target == nums[mid]) | |
return mid; | |
if(nums[lo] <= nums[mid]){ | |
if(target < nums[mid] && target >= nums[lo]) | |
hi = mid - 1; | |
else | |
lo = mid + 1; | |
} | |
else{ | |
if(target > nums[mid] && target <= nums[hi]) | |
lo = mid + 1; | |
else | |
hi = mid - 1; | |
} | |
} | |
return -1; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment