leetcode -. 35 search insertion position

 1 class Solution:
 2     def searchInsert(self, nums, target: int) -> int:
 3         if target in nums:
 4             return nums.index(target)
 5         else:
 6             if target>=nums[-1]:
 7                 return len(nums)
 8             if target<nums[0]:
 9                 return 0
10             for i in range(len(nums)):
11                 if target>=nums[i] and target<nums[i+1]:
12                     return i+1
When execution: 88 ms, beat the 26.55% of users in all Python3 submission
Memory consumption: 14.2 MB, beat the 5.47% of users in all Python3 submission
 
                                                                                                ——  2019.9.24

Guess you like

Origin www.cnblogs.com/taoyuxin/p/11577016.html