注:最后有面试挑战,看看自己掌握了吗
二分查找Binary Search
终于做出来了,实在太生疏了,就一个=号,要了我的亲命!
while low<=high:
def binarysearch(list,a): print(list,a) low=0; high=len(list)-1 a=int(a) while low<=high: mid =int( (low + high) / 2) print(mid,low,high) if list[mid]==a: return mid elif list[mid]>a: high=mid-1 elif list[mid]<a: low = mid+1 if '__main__' == __name__: list = [2,6,8,9,14] a= 9 print(binarysearch(list,a)) [2, 6, 8, 9, 14] 9 2 0 4 3 3 4 3
🌸I could be bounded in a nutshell and count myself a king of infinite space.
特别鸣谢:木芯工作室 、Ivan from Russia