My Accepted Java Solution


  • 0
    A
    public void merge(int A[], int m, int B[], int n) {
            int insertIndex = m+n-1;
            int indexA = m-1,indexB = n-1;
            
            while(indexB>=0){
                if(indexA<0){
                    A[insertIndex--] = B[indexB--];
                }else{
                    if(B[indexB]>=A[indexA]){
                        A[insertIndex--] = B[indexB--];
                    }else{
                        A[insertIndex--] = A[indexA--];
                    }
                }
            }
        }

  • 0
    T
    public class Solution {
        public void merge(int A[], int m, int B[], int n) {
            int i = m-1, j = n-1, k = m+n-1;
            
            while(i >=0 && j >= 0) {
                A[k--] = A[i] > B[j] ? A[i--] : B[j--];
            }
            
            while(j >= 0)
                A[k--] = B[j--];
        }
    }

Log in to reply
 

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