035 Search Insert Position

二分法, 最后注意一下输出的时候要和 nums[left]比较一下来决定输出多少

class Solution:
    # @param {integer[]} nums
    # @param {integer} target
    # @return {integer}
    def searchInsert(self, nums, target):
        return self.help(nums, target, 0, len(nums)-1)

    def help(self, a, t, left, right):
        while left < right:
            mid = (left + right) / 2
            if a[mid] == t:
                return mid
            elif a[mid] < t:
                left = mid + 1
            else:
                right = mid - 1
        if t > a[left]:
            return left + 1
        else:
            return left

 

posted @ 2015-07-08 13:50  dapanshe  阅读(97)  评论(0编辑  收藏  举报