python实现leetcode-第35题-搜索插入位置

class Solution:
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        n = 0
        for i in range(len(nums)):
            if nums[i] < target:
                n = n+1
            elif nums[i] == target:
                n = i
                return n
            elif nums[i] > target:
                return n
        return n

简单粗暴的方法,依次比较目标值与数组数据,n作为当前应返回的数值。5分钟写完代码,提交后可以通过,最低执行用时: 44 ms, 在Search Insert Position的Python3提交中击败了99.45% 的用户,万万没想到...

补充:可以用二分法缩短时间。

全部评论

相关推荐

合适才能收到offe...:些许风霜罢了查看图片
点赞 评论 收藏
分享
04-15 09:59
门头沟学院 C++
yy_11:小公司人家没必要泄密,大公司都是本地部署了
你想吐槽公司的哪些规定
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务