要求:给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
#enumerate()函数会输出下标和对象
for i,num in enumerate(nums):#没超过列表边界
if target<=num:
return i
return len(nums)#若超出
#len()函数返回对象(字符、列表、元组等)长度或项目个数