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

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

示例 1:

输入: [1,3,5,6], 5
输出: 2

示例 2:

输入: [1,3,5,6], 2
输出: 1

示例 3:

输入: [1,3,5,6], 7
输出: 4

示例 4:

输入: [1,3,5,6], 0
输出: 0
class Solution:
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """

        index=-1
        last=0
        try:
            return nums.index(target)
        except:
            for i in range(len(nums)):
                if target > nums[i]:
                    last+=1
        nums.insert(last,target)
        return index if index > 1 else last

 



相关文章:

  • 2022-01-20
  • 2021-08-27
  • 2021-12-10
  • 2021-10-23
  • 2021-11-16
  • 2022-12-23
  • 2022-02-28
  • 2021-08-14
猜你喜欢
  • 2021-10-29
  • 2021-12-25
  • 2021-09-01
  • 2022-12-23
  • 2021-07-10
  • 2021-12-27
相关资源
相似解决方案