# Leetcode 搜索插入位置
# 该题我依然使用循环遍历去解决 注意边界问题 还是有点小复杂的 应该有更好的方式 有机会探讨一下
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
if nums == []:
return -1
elif len(nums) == 1:
if target <= nums[0]:
return 0
else:
return 1
else:
if target >= nums[-1]:
if target == nums[-1]:
return len(nums) - 1
else:
return len(nums)
elif target < nums[0]:
return 0
else:
length = len(nums) - 1
i = 0
while i + 1 <= length:
if nums[i] == target:
return i
elif nums[i] < target and nums[i + 1] > target:
return i + 1
i += 1