Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note:
You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
Analysis: In-place merge starts from the end of A.
public class Solution {
public void merge(int A[], int m, int B[], int n) {
int count = m + n - 1;
m -= 1;
n -= 1;
while(m >= 0 && n >= 0) {
A[count--] = A[m] >= B[n] ? A[m--] : B[n--];
}
while(n >= 0) {
A[count--] = B[n--];
}
return;
}
}