leetcode刷题-查找

二分查找

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刷题-查找)