注意事项
- python2 使用 /
- python3 使用 //
例子
- 二分法取数值
def binary_search(search_list,target):left = 0right = len(search_list) - 1while left <= right:mid = (left + right) // 2if search_list[mid] < target:left = mid +1continueif search_list[mid] == target:return midif search_list[mid] > target:right = mid - 1return None
search_list = [1,3,4,6,8,9]
print(binary_search(search_list,5))
print(binary_search(search_list,1))
print(binary_search(search_list,3))
print(binary_search(search_list,4))
print(binary_search(search_list,6))
print(binary_search(search_list,8))
print(binary_search(search_list,9))