Created
July 2, 2019 12:00
-
-
Save Aloxaf/aaaedea491292289c3faca147ac5dfc6 to your computer and use it in GitHub Desktop.
二分查找
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 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