python写算法题:leetcode: 35. Search Insert Position

来源:互联网 发布:网络蜘蛛磁力链 编辑:程序博客网 时间:2024/06/15 19:04

https://leetcode.com/problems/search-insert-position/description/

class Solution(object):    def searchInsert(self, nums, target):        """        :type nums: List[int]        :type target: int        :rtype: int        """        if len(nums) == 0:            return 0        start = 0        end = len(nums)-1        while start<end:            half = (start+end)/2            if nums[half]<target:                start = half+1            else:                end = half-1        if nums[start] >= target:            return start        return start+1