35. Search Insert Position

2019-03-05  本文已影响0人  FlyCharles

1. 我的AC

方法一

class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if target in nums:
            return nums.index(target)
        else:
            for i in range(len(nums)):
                if target < nums[i]:
                    return i
            return i + 1

方法二

class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        return len([num for num in nums if num < target])
上一篇下一篇

猜你喜欢

热点阅读