美文网首页Leetcode
35. Search Insert Position

35. Search Insert Position

作者: oo上海 | 来源:发表于2016-07-29 10:20 被阅读5次

35. Search Insert Position

题目:
https://leetcode.com/problems/search-insert-position/

难度:

Medium

递归

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

相关文章

网友评论

    本文标题:35. Search Insert Position

    本文链接:https://www.haomeiwen.com/subject/nnhzjttx.html