Skip to content

Instantly share code, notes, and snippets.

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 spencerxhani/f9d732374cfff9504f77a81089749a01 to your computer and use it in GitHub Desktop.
Save spencerxhani/f9d732374cfff9504f77a81089749a01 to your computer and use it in GitHub Desktop.
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