Java 190ms no additional data structures.


  • 0
    2
    public void merge(int A[], int m, int B[], int n) {
        int resultIndex = 0;
        int aIndex = m - 1;
        int bIndex = n - 1;
        
        for(resultIndex = m + n - 1 ; resultIndex >= 0 ; resultIndex--){
            if(bIndex < 0){
                //Sorting complete. remaining numbers in A are already sorted
                break;
            }
            if(aIndex >= 0 && A[aIndex] >= B[bIndex]){
                A[resultIndex] = A[aIndex];
                aIndex--;
            }else{
                A[resultIndex] = B[bIndex];
                bIndex--;
            }
        }
        
    }

Log in to reply
 

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