二分法,太容易写出bug了!
def binary_search(a, v):
l, r = 0, len(a)
while l + 1 < r:
m = (l + r) // 2
if a[m] <= v:
l = m
else:
r = m
# 通过a[l] == v判断v不存在与a数组当中的情况
return l
def binary_search(a, v):
l, r = 0, len(a) - 1
while l <= r:
m = (l + r) // 2
if a[m] == v:
return m
if a[m] < v:
l = m + 1
else:
r = m - 1
# 表示不存在
return -1
评论