给定排序数组和目标值,如果找到目标,则返回索引。 如果没有,请返回索引按顺序插入的索引。
您可以假设数组中没有重复项。
Example 1:
Input: [1,3,5,6], 5
Output: 2
Example 2:
Input: [1,3,5,6], 2
Output: 1
Example 3:
Input: [1,3,5,6], 7
Output: 4
具体的Python 代码如下:
def searchNum(aList,target):
for i in aList:
if i>=target:
return alist.index(i)
if aList[0]>=target:
return 0
else:
return len(aList)
if __name__ == '__main__':
alist=[1,3,5,6]
target=2
numIndex=searchNum(alist,target)
print(numIndex)