时间限制:1秒 空间限制:32768K 热度指数:8547
题目描述
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 to hold additional elements from B. The number of elements initialized in A and B are m and n
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int sum=n+m;
int k=m-1,j=n-1;
for(int i=sum-1;i>=0;i--){
if(k>=0&&j>=0)A[i]=A[k]>B[j]?A[k--]:B[j--];
else if(k>=0){A[i]=A[k];k--;}
else {A[i]=B[j];j--;}
}
}
};