Python solution using bisect for binary search

  • 0
    from bisect import bisect_left, bisect_right
    def searchRange(nums, target):
        left = bisect_left(nums, target)
        if left >= len(nums) or nums[left] != target:
            left = -1
        right = bisect_right(nums, target) - 1
        if right < 0 or nums[right] != target:
            right = -1
        return [left, right]

    Making use of for fast binary search.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.