Skip to content

Instantly share code, notes, and snippets.

@liondancer
Created January 10, 2017 00:07
Show Gist options
  • Save liondancer/3240bbdbd31487a1bdb0f7f7b89d6a49 to your computer and use it in GitHub Desktop.
Save liondancer/3240bbdbd31487a1bdb0f7f7b89d6a49 to your computer and use it in GitHub Desktop.
def searchRange(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
if nums:
return search(nums, 0, len(nums) - 1, target)
return [-1,-1]
def search(nums, L, R, target):
if nums[L] == target and nums[R] == target:
return [L, R]
if nums[L] <= target and target <= nums[R]:
mid = (L + R) // 2
left = search(nums, L, mid, target)
right = search(nums, mid + 1, R, target)
if left[0] == -1:
return right
if right[0] == -1:
return left
return [left[0], right[1]]
return [-1,-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment