[CareerCup] 11.1 Merge Arrays 合并数组

时间:2022-04-23 22:56:11

11.1 You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted orde.

LeetCode上的原题,请参见我之前的博客Merge Sorted Array 混合插入有序数组

class Solution {
public:
void merge(vector<int> &a, int m, vector<int> &b, int n) {
int cnt = m + n - ;
--m; --n;
while (m >= && n >= ) a[cnt--] = a[m] > b[n] ? a[m--] : b[n--];
while (n >= ) a[cnt--] = b[n--];
}
};