class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
# 二分法
left = 0
right = len(nums)-1
while left <= right:
mid = left + ((right - left) >>1)
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid -1
return -1
70-二分查找法--LC704二分查找
最新推荐文章于 2024-08-15 05:02:54 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)