As long as I'm allowed to use bisect in Python[3 Lines]


  • 0

    I believe a built-in function of Python Bisect will help a lot here.

    class Solution(object):
        def searchRange(self, nums, target):
            left, right = bisect.bisect_left(nums, target), bisect.bisect_right(nums, target)
            left, right = left if left < len(nums) and nums[left] == target else - 1, right - 1 if nums[right - 1] == target else -1
            return [left, right]
    

Log in to reply
 

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