Leetcode 792 Solution

This article provides solution to leetcode question 792 (binary-search)

https://leetcode.com/problems/binary-search

Solution

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