public void merge(int A[], int m, int B[], int n) {
int i=m1, j=n1, k=m+n1;
while (i>1 && j>1) A[k]= (A[i]>B[j]) ? A[i] : B[j];
while (j>1) A[k]=B[j];
}
3 line Java Solution


public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {ArrayList<Integer> res = new ArrayList<Integer>(); // Arrays.copyOf(nums1, m + n); for(int num : nums2){ nums1[m++] = num; } Arrays.sort(nums1); for(int num : nums1){ if(res.contains(num)){ res.remove(num); } res.add(num); } int i = 0; for(int num : res){ nums1[i++] = num; }
Is anyone can help me whats wrong wiht my code?
}
}

@soloallen only the following code is required.
public class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { for(int i = 0; i < n; i++) nums1[i+m] = nums2[i]; Arrays.sort(nums1); } }
Also, I think its better to use sorted set instead of ArrayList.


said in 3 line Java Solution:
while (j>1) A[k]=B[j];
Nice solution. Thanks. Could anybody please explain why we need this statement:
while (j>1) A[k]=B[j];
Thanks.

@ynys Never mind, I figured out, for this case.
int[] a = {8, 9, 10, 11, 0, 0, 0,0,0,0,0};
int[] b = {1,2,3,4,5};Thanks.


@fabrizio3 said in 3 line Java Solution:
while(n>0)A[m+n1]=(m==0B[n1]>A[m1])?B[n]:A[m];
Hey! Don't waste lines please! We just need 1 :)