边工作边刷题: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
        
        
posted @ 2016-04-13 11:43  absolute100  阅读(86)  评论(0编辑  收藏  举报