Python, Binary Search, beats 99%

  • 0


    def searchRange(self, nums, target):
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        i = self.binarySearch(0, len(nums)-1,nums, target) 
        if i == -1:
            return [i, i]
        j, k = i, i
        while j >= 0:
            j1 = j
            j = self.binarySearch(0, j1-1, nums, target)
        while k >= 0:
            k1 = k
            k = self.binarySearch(k1+1, len(nums)-1, nums, target)
        return [j1, k1]
    def binarySearch(self, begin, end, nums, target):
        if begin > end:
            return -1
        mid = (begin+end)/2
        if nums[mid] == target:
            return mid
        elif nums[mid] < target:
            return self.binarySearch(mid+1, end, nums, target)
            return self.binarySearch(begin, mid-1, nums, target)


Log in to reply

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