Accepted O(logN) solution Python


  • 0
    X
    class Solution:
        # @param A, a list of integers
        # @param target, an integer to be inserted
        # @return integer
        def searchInsert(self, A, target):
            start = 0
            end = len(A)-1
            while start <= end:
                mid = (start+end)//2
                if A[mid] == target:
                    return mid
                if A[mid] > target:
                    if mid == 0:
                        return mid
                    if A[mid-1] < target:
                        return mid
                    else:
                        end = mid-1
                        continue
                if A[mid] < target:
                    start = mid+1
                    continue
            return end+1

Log in to reply
 

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