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 (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and nrespectively.
A已经有足够的空间,且A,B元素个数已知,重复的数字不删除,为了移动的元素尽量少,从A的后端由大到小插入,如果A先到头将B剩下的元素写入A中,若B先到头则A剩下的元素已经就位,不需要在处理。
public class Solution {
public void merge(int A[], int m, int B[], int n) {
if(n==0)return;
int i=m-1,j=n-1,k=m+n-1;
while(i>=0&& j>=0)
{
if(A[i]<B[j])A[k--]=B[j--];
else A[k--]=A[i--];
}
if(i<0)while(j>=0)A[k--]=B[j--];
}
}