Accepted O(logN) solution Python

  • 0
    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
                        end = mid-1
                if A[mid] < target:
                    start = mid+1
            return end+1

Log in to reply

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