# 1 Line Solution

• ``while(n>0) A[m+n-1] = (m==0||B[n-1] > A[m-1]) ? B[--n] : A[--m];``

• This post is deleted!

• ``````public static void merge(int A[], int m, int B[], int n) {
while(n>0)A[m+n-1]=(m==0||B[n-1]>A[m-1])?B[--n]:A[--m];
}
public static void printArray(int[] a) {
for(int i: a) System.out.print(i+" ");
System.out.println();
}
public static void main(String[] args) {
int[] A = {4,5,6,0,0,0};
int m = 3;
int[] B = {1,2,3};
int n = 3;
printArray(A);
printArray(B);
merge(A,m,B,n);
printArray(A);
}``````

• Hi, I added some code in an answer to test the input you suggested, so you can try it

• sorry, I misunderstood your code, this is correct and smart! thanks for sharing the solution!

• really crazy, so amazing

• A={1,3,5,7,9,0,0,0,0}， m=5
B={10,8,4,2}, n=4,

result:1 10 8 4 2 3 5 7 9

• @spade-0 they are two sorted integer arrays...

• @jediknight23 But if the test case is nums1={1,2}, m=2, nums2={3},n=1, the code doesn't work.......

• It's so amazing, and it supply a new method to simplify codes.Thank you.
However,I think it's not good for using it in this problem.It makes side effects appearing,and it depends the concrete compiler's interpreting.I look over it in the book <The C Programming Language> just now.In the ending of chapter 2,it not recommended us writing codes like this.

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