美文网首页
leetcode刷题-查找

leetcode刷题-查找

作者: 噗嗤噗哩噗通 | 来源:发表于2021-12-29 11:38 被阅读0次

二分查找

class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        high=len(nums)-1
        low=0
        while high>=low:
            n= ( high - low ) / 2 + low
            if nums[n] == target:
                return n
            elif nums[n] < target:
                low =  n+1
            else:
                high =  n-1
            
        return -1

相关文章

网友评论

      本文标题:leetcode刷题-查找

      本文链接:https://www.haomeiwen.com/subject/pvemqrtx.html