Skip to content

Latest commit

 

History

History
32 lines (23 loc) · 537 Bytes

704.md

File metadata and controls

32 lines (23 loc) · 537 Bytes

Binary Search

Description

link


Solution

  • See Code

Code 2

O(log(n))

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        l, r = 0, len(nums) - 1
        while l < r:
            m = (l + r) // 2
            if nums[m] == target:
                return m
            if nums[m] < target:
                l = m + 1
            else:
                r = m
        return l if nums[l] == target else -1