Skip to content

Instantly share code, notes, and snippets.

@GloryAlex
Created April 20, 2022 06:55
Show Gist options
  • Save GloryAlex/86f97aee8a42532dad1ab28426b01110 to your computer and use it in GitHub Desktop.
Save GloryAlex/86f97aee8a42532dad1ab28426b01110 to your computer and use it in GitHub Desktop.
标准库的实现
# 求左闭右开非降序区间 [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