leetcode: 搜索插入位置

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

你可以假设数组中无重复元素。


好的,简单题终结者石锤了= =||

这题就是一遍遍历过去就完事了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
for i in range(len(nums)):
if nums[i] == target:
return i
elif nums[i] > target:
return i

return len(nums)

我不想咸鱼啊,可是大脑他不允许啊艹!明天又大一岁了,还是努点力吧 : - (

【在,为什么咸鱼!】

【因为懒啊!】