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) {
int ia = m - 1;
int ib = n - 1;
int ind = m + n - 1;
while (ia >= 0 && ib >= 0) {
A[ind--] = A[ia]>B[ib]?A[ia--]:B[ib--];
}
while (ib >= 0) {
A[ind--] = B[ib--];
}
}
};
本文介绍了一个C++函数,该函数可以将两个已排序的整数数组B合并到A中,形成一个新的有序数组。通过从两个数组的末尾开始比较并放置较大元素的方式,实现了高效的合并过程。
918

被折叠的 条评论
为什么被折叠?



