给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
请必须使用时间复杂度为 O(log n) 的算法。
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
n = len(nums) - 1
for i in nums :
if i >= target :
return nums.index(i)
elif nums[0] > target :
return 0
return len(nums)