Python solution using bisect for binary search


  • 0
    M
    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 https://docs.python.org/2/library/bisect.html for fast binary search.


Log in to reply
 

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