704. 二分法查找

class Solution:

def search(self, nums: List[int], target: int) -> int:

if target in nums:

return nums.index(target)

else:

return -1

没用二分法偷跑偷跑居然滑过了

def search(nums, target) -> int:

l, r = 0, len(nums)

mid = (l + r) // 2

while l < r:

mid = (l + r) // 2

if nums[mid] == target:

return mid

elif nums[mid] < target:

l = mid + 1

elif nums[mid] > target:

r = mid

return -1


假装用了二分法。。。 l = mid + 1来结束loop