Python, binary search


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

Log in to reply
 

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