Python solution


  • 0
    A
    def searchInsert(self, nums, target):
        if nums == None:
            return -1
        if len(nums) == 0 or nums[0] > target:
            return 0
        if nums[-1] < target:
            return len(nums)
        start = 0
        end = len(nums) - 1
        while(start + 1 < end):
            mid = start + (end - start) / 2
            if nums[mid] >= target:
                end = mid
            else:
                start = mid + 1
        if nums[start] == target:
            return start
        if nums[end] == target:
            return end
        return start if nums[start] > target else start + 1

Log in to reply
 

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