Posted on 2023-04-01 16:20
Uriel 阅读(32)
评论(0) 编辑 收藏 引用 所属分类:
闲来无事重切Leet Code 、
大水题 、
二分.三分
实现二分查找,大水题
1 #704
2 #Runtime: 178 ms (Beats 94.20%)
3 #Memory: 14.8 MB (Beats 31.81%)
4
5 class Solution(object):
6 def search(self, nums, target):
7 """
8 :type nums: List[int]
9 :type target: int
10 :rtype: int
11 """
12 l = 0
13 r = len(nums)
14 while l < r:
15 mid = (l + r) // 2
16 if nums[mid] == target:
17 return mid
18 if nums[mid] < target:
19 l = mid + 1
20 else:
21 r = mid
22 return -1