题解 | #二分查找-I#
二分查找-I
http://www.nowcoder.com/practice/d3df40bd23594118b57554129cadf47b
由于题目说给定一个元素升序的,无重复数字的数组,所以排除空数组的可能 class Solution: def Search(self,nums:List[int],target:int)->int: left = 0 right = len(nums) while left <= right: middle = (left + right ) // 2 if target == nums[middle]: return middle if target > nums[middle]: left = middle + 1 -> 左区间下标要+1 if target < nums[middle]: right = middle -1 -> 右区间下标要-1 return -1