每日一道Leetcode - 704. 二分查找【二分查找】

每日一道Leetcode - 704. 二分查找【二分查找】_第1张图片

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        left, right = 0, len(nums)-1
        while left <= right:
            # mid value
            pivot = left + (right-left) // 2
            if nums[pivot] == target:
                return pivot
            if target < nums[pivot]:
                right = pivot - 1
            else:
                left = pivot + 1
        return -1


你可能感兴趣的:(Leetcode,leetcode,二分法)