Traight binary search solution with Python


  • 0
    G
    enter code hereclass Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if target<nums[0]:
            return 0
        if target>nums[len(nums)-1]:
            return len(nums)
        
        s=0
        t=len(nums)-1
        while s<t:
            mid=s+(t-s)//2
            if nums[mid]<target:
                s=mid+1
            else:
                t=mid
        return s

Log in to reply
 

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