Skip to content

Instantly share code, notes, and snippets.

@wilderfield
Last active May 8, 2021 18:00
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 wilderfield/7ce1dfc775a142769195798fece454b4 to your computer and use it in GitHub Desktop.
Save wilderfield/7ce1dfc775a142769195798fece454b4 to your computer and use it in GitHub Desktop.
Python Binary Search
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
l = 0
r = len(nums)-1
while l <= r:
m = l+(r-l)/2
if nums[m] == target:
return m
if nums[m] < target:
l = m+1
else:
r = m -1
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment