Created
August 2, 2020 00:33
-
-
Save spencerxhani/f9d732374cfff9504f77a81089749a01 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: | |
def search(self, nums, target): | |
""" | |
:type nums: List[int] | |
:type target: int | |
:rtype: int | |
""" | |
start, end = 0, len(nums) - 1 | |
while start <= end: | |
mid = start + (end - start) // 2 # mid is a pivot index | |
if nums[mid] == target: | |
return mid | |
elif nums[mid] >= nums[start]: | |
if nums[mid] > target and nums[start] <= target: | |
end = mid -1 | |
else: | |
start = mid +1 | |
else: # nums[mid] < nums[start]: | |
if nums[mid] < target and nums[end] >= target: | |
# need to includes start or end , corner case | |
start = mid +1 | |
else: | |
end = mid -1 | |
return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment