My python3 solution


  • 0
    Y
    class Solution(object):
        def searchInsert(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: int
            """
            return self.binary_search(nums, target)
    
        def binary_search(self, nums, target):
            i = 0
            j = len(nums) - 1
            while i <= j:
                mid = (i + j) // 2
                if nums[mid] == target:
                    return mid
                elif nums[mid] > target:
                    j -= 1
                else:
                    i += 1
            return i
    

Log in to reply
 

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