python search the position 35ms


  • 0
    B

    class Solution(object):
    def searchInsert(self, nums, target):
    """
    :type nums: List[int]
    :type target: int
    :rtype: int
    """

        if nums[len(nums)-1] == target:
            return len(nums)-1
        if nums[len(nums)-1] < target:
            return len(nums)
        if nums[0] >= target:
            return 0
        tmp_nums = nums
        index_small = 0
        index_big = len(nums)-1
        
        while True:
            mid_num = tmp_nums[len(tmp_nums)/2]
            if mid_num == target:
                return nums.index(mid_num)
            elif mid_num < target:
                index_small = nums.index(mid_num)
                tmp_nums = nums[index_small:index_big+1]
            elif mid_num > target:
                index_big = nums.index(mid_num)
                tmp_nums = nums[index_small:index_big+1]
                if len(tmp_nums) == 2:
                    return (index_small+1)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.