边工作边刷题:70天一遍leetcode: day 35-3

Search Insert Position

class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if target>nums[-1]: return len(nums)
        if target<nums[0]: return 0
        
        low,high=0,len(nums)-1
        mid = low+(high-low)/2
        while low<high:
            if nums[mid]>=target:
                high=mid
            else:
                low=mid+1
            mid = low+(high-low)/2
        
        return mid
        
        
原文地址:https://www.cnblogs.com/absolute/p/5678230.html