Created
April 20, 2022 06:55
-
-
Save GloryAlex/86f97aee8a42532dad1ab28426b01110 to your computer and use it in GitHub Desktop.
标准库的实现
This file contains 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
# 求左闭右开非降序区间 [left, right) 中第一个大于或等于 target 的位置 | |
def lower_bound(array, left, right, target): | |
while left < right: | |
mid = left + (right - left) // 2 # 防止溢出 | |
if array[mid] < target: left = mid + 1 | |
# if !(target < array[mid]): # 小于等于即为 upper_bound | |
else: right = mid | |
return left # 此时 left==right 成立 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment