Python - Binary search solution


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

Log in to reply
 

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