Concise Python Code


  • 0
    class Solution(object):
        def searchInsert(self, nums, target):
            low, high = 0, len(nums) - 1
            while (low < high):
                mid = (low + high) // 2
                if nums[mid] == target:
                    return mid
                if nums[mid] < target:
                    low = mid + 1
                else:
                    high = mid - 1
            return low if target <= nums[low] else low + 1     #here, low == high, we need only to compare nums[low] and target
    

Log in to reply
 

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