#给定一个排序的整数数组和目标整数,输出其所在位置 class Solution: #参数nums #target:要查找的目标 def search(self, nums, target): for i,v in enumerate(nums): if v == target: return i return -1 if __name__ == '__main__': my_solution = Solution() nums = [1,2,3,4,5,6] target = 0 targetIndex = my_solution.search(nums, target) print("输入nums = ", nums," ","target = ", target) print("输出 = ", targetIndex) D:\ANACONDA\envs\pytorch_gpu\python.exe E:/Python面试准备/python基础/练习/300.py 输入nums = [1, 2, 3, 4, 5, 6] target = 0 输出 = -1 Process finished with exit code 0 def binary_serch(l,val): left = 0 right = len(l)-1 while left <= right: mid = (left + right) // 2 if l[mid] == val: return mid elif l[mid] > val: right = mid -1 else: left = mid +1 else: return None