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 mand n respectively.
AB两个数组已经排好序,现在要求合并两数组并排好序放入A中,A的空间足够大。这里主要考排序的知识,对于各种排序方法我还得好好加深理解。
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int i = 0;
int j = 0;
int k = 0;
int c[m+n];
while(i<m && j<n)
{
if(A[i]>B[j])
c[k++] = B[j++];
else
c[k++] = A[i++];
}
while(i<m)
c[k++] = A[i++];
while(j<n)
c[k++] = B[j++];
for(int w = 0; w < k; w++)
{
A[w] = c[w];
}
}
};