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 andn respectively.
分析:归并排序的一部分
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
vector<int> a;
int i = 0;
int j = 0;
while(i < m && j < n)
{
if(A[i] < B[j])
a.push_back(A[i++]);
else
a.push_back(B[j++]);
}
while(i < m)
a.push_back(A[i++]);
while(j < n)
a.push_back(B[j++]);
for(int k = 0; k < m+n; k++)
{
A[k] = a[k];
}
return;
}
};