Leetcode 分享筆記

35. Search Insert Position

題目

leetcode
b

python


class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        # 找到數字即跳出索引,未找到數字則找到排序之索引
        for i in range(0,len(nums)):
            if (nums[i] == target):
                return i
            if (i == len(nums)-1):
                if(target <= nums[0]):
                    return 0
                return len(nums)
            elif (nums[i] < target <= nums[i+1]):
                return i+1