Skip to content

Latest commit

 

History

History
18 lines (17 loc) · 467 Bytes

704. Binary Search.md

File metadata and controls

18 lines (17 loc) · 467 Bytes

704. Binary Search (Easy)

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