35. Search Insert Position
2019-03-05 本文已影响0人
FlyCharles
1. 我的AC
方法一
class Solution(object):
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 in range(len(nums)):
if target < nums[i]:
return i
return i + 1
方法二
- 序号 = 数组中小于目标数的个数
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
return len([num for num in nums if num < target])