Leetcode 35インデックス挿入位置


# 20200520

"""
              ,         ,      。            ,              。
             。

   1:
  : [1,3,5,6], 5
  : 2

   2:
  : [1,3,5,6], 2
  : 1

   3:
  : [1,3,5,6], 7
  : 4

   4:
  : [1,3,5,6], 0
  : 0
"""

"""
  :    ,     
   :
    :      ,  0
         , enumerate  
     ,      
     :
             ,     【         】
       ,     
    
     :
     :O(N)
     :O(1)

   :    

     :
     :O(logN)
     :O(1)
"""

class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        #     :      ,  0
        if not nums:
            return 0
        for idx,num in enumerate(nums):
            if num >= target:
                return idx
        return len(nums)


s = Solution()
print(s.searchInsert([], 5))
print(s.searchInsert([1,3,5,6], 5))
print(s.searchInsert([1,3,5,6], 2))
print(s.searchInsert([1,3,5,6], 7))
print(s.searchInsert([1,3,5,6], 0))