leetcode算法题--搜索插入位置 python

题目概述:
leetcode算法题--搜索插入位置 python_第1张图片
python钟append()函数可以插入相应数值,默认插入在末端,因此在列表中插入target,然后通过sort()函数从小到大进行排序,通过索引就可以获得该下标。

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        nums.append(target)#直接插入相关数据
        nums.sort()#在排序
        return nums.index(target)

结果展示:
leetcode算法题--搜索插入位置 python_第2张图片
关键之处:联想到sort(),append()函数就可以快速解决。
另外也可以通过二分法,以及顺序查找做出

你可能感兴趣的:(leetcode算法题--搜索插入位置 python)