35. Search Insert Position

class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if len(nums) == 0:
            return 0
        
        start, end = 0, len(nums) - 1
        while start + 1 < end:
            # [1, 3, 3, 3, 5, 6]
            #  s              e
            mid = start + (end - start) / 2
            if nums[mid] < target:
                start = mid
            else:
                end = mid
        if nums[start] == target:
            return start
        if nums[end] == target:
            return end
        return len(nums)Search Insert Position

Last updated