Skip to content

Instantly share code, notes, and snippets.

@Aloxaf
Created July 2, 2019 12: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 Aloxaf/aaaedea491292289c3faca147ac5dfc6 to your computer and use it in GitHub Desktop.
Save Aloxaf/aaaedea491292289c3faca147ac5dfc6 to your computer and use it in GitHub Desktop.
二分查找
def lower_bound(array, first, last, value): # 返回[first, last)内第一个不小于value的值的位置
while first < last: # 搜索区间[first, last)不为空
mid = first + (last - first) // 2 # 防溢出
if array[mid] < value:
first = mid + 1
else:
last = mid
return first # last也行,因为[first, last)为空的时候它们重合
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment