My solution of binary search


  • 1
    Y

    int searchInsert(int A[], int n, int target) {

        int l = 0;
        int r = n-1;
        int mid;
       //binar search to update search range
        while(l<r)
        {
            mid = (l+r)>>1;
            if(A[mid]<target)
                l =mid+1;
            else
                r=mid;
        }
       return A[l]>=target?l:l+1;
    }

Log in to reply
 

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