Last active
May 8, 2021 18:00
-
-
Save wilderfield/7ce1dfc775a142769195798fece454b4 to your computer and use it in GitHub Desktop.
Python Binary Search
This file contains hidden or 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
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