题解 | #二分查找-I#
二分查找-I
http://www.nowcoder.com/practice/d3df40bd23594118b57554129cadf47b
二分查找法,定义start和end作为搜索范围,根据start和end来确定index
class Solution:
def search(self , nums: List[int], target: int) -> int:
# write code here
nums_len = len(nums)
start = 0
end = nums_len - 1
while start <= end:
index = int((start + end) / 2)
if nums[index] == target:
return index
elif nums[index] < target:
start = index + 1
else:
end = index - 1
return -1