Python3 Solution


  • 0
    M
    class Solution:
        def searchInsert(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: int
            """
            if target not in nums:
                nums.append(target)
                nums.sort()
            return nums.index(target)
    

  • 0
    V
    class Solution:
        def searchInsert(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: int
            """
            if target in nums:
                return nums.index(target)
            else:
                for i, n in enumerate(nums):
                    if n > target:
                        return i
                return len(nums)
    

Log in to reply
 

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