题目:
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.
题意要把两个有序的数组合并到他们中的一个数组中。
唯一的技巧就是从结尾开始归并,不会覆盖元素又能满足题意。class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int i,j,k;
for(i=m-1,j=n-1,k=m+n-1;k>=0;--k)
{
if(i>=0&&(j<0||A[i]>B[j]))
{
A[k]=A[i--];
}
else A[k]=B[j--];
}
}
};