Skip to content

704. Binary Search

Companies: Facebook, Microsoft
Date: September 7, 2021
LeetCode link: https://leetcode.com/problems/binary-search/
Review Date: October 3, 2021

class Solution:

    def search(self, nums: List[int], target: int) -> int:

        start = 0
        end = len(nums) - 1
        while start <= end:
            mid = (start + end) // 2
            if nums[mid] == target:
                return mid
            elif target < nums[mid]:
                end = mid - 1
            else:
                start = mid + 1

        return -1

Last update : 25 mai 2024
Created : 25 mai 2024