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.
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int temp[m + n];int pa, pb, pt;
for (pa = pb = pt = 0; pa < m || pb < n; ) {
if ((pb == n) || (pa < m && A[pa] < B[pb])) temp[pt++] = A[pa++];
else if ((pa == m) || (pb < n && A[pa] >= B[pb])) temp[pt++] = B[pb++];
}
for (int i = 0; i < m + n; i++) A[i] = temp[i];
}
};