Skip to content

Instantly share code, notes, and snippets.

@Hanaasagi
Created May 4, 2017 05:08
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 Hanaasagi/2872fc7b6dd9fbd2c42f31b614be34ef to your computer and use it in GitHub Desktop.
Save Hanaasagi/2872fc7b6dd9fbd2c42f31b614be34ef to your computer and use it in GitHub Desktop.
可以找出元素第一次出现位置的二分查找
def binary_search(l, t):
low, high = -1, len(l)
while low+1 != high:
mid = (low + high) / 2
if l[mid] < t:
low = mid
else:
high = mid
pos = high
if pos >= len(l) or l[pos] != t:
pos = -1
return pos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment