I can't pass the test input A=[] and B=[1] (Merge Sorted Array)


  • 0
    B
    public static void merge(int A[], int m, int B[], int n) {
    	int[] C=new int[m+n];
    	for (int i=0;i<A.length;i++){
    		C[i]=A[i];
    	}
    	for (int i=A.length;i<C.length;i++){
    		C[i]=B[i-A.length];
    	}
    	Arrays.sort(C);
    }
    

    My computer tested that case and the answer is 1 but online judgement is 0. I don't know why. Anyone could help?


  • 0
    W

    You should not create a new array to merge A and B together. You should only use A and B, then merge B into A. Then you can pass the online test. I wish my answer can help you.


  • 0
    B

    Cheers, I passed when I followed your advice.


  • 0
    R

    if(m == 0) {

    	System.arraycopy(B,0,A,0,n);
    	return;
    }
    if( n == 0)
    	return;
    System.arraycopy(B,0,A,m,n);
    Arrays.sort(A);
    

    I followed your code and accepted!But this is not what the system expected!


Log in to reply
 

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