Python's version of solution (beat 100% of submit time)


  • 0
    class Solution:
        def searchInsert(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: int
            """
            self.nums = nums
            self.target = target
            
            #check if target is in list:
            if target in nums:
                return nums.index(target)
            
            #if not in list, then we need to insert the target nbr into list:
            else:
                nums.append(target)
                nums.sort()
                return nums.index(target)
    

Log in to reply
 

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