从后向前,避免移位操作
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int Index=m+n-1;
int aIndex=m-1;
int bIndex=n-1;
while(aIndex>=0 && bIndex>=0){
if(A[aIndex]>=B[bIndex]){
A[Index]=A[aIndex];
aIndex--;
}else{
A[Index]=B[bIndex];
bIndex--;
}
Index--;
}
if(bIndex!=-1){
while(bIndex>=0){
A[Index]=B[bIndex];
bIndex--;
Index--;
}
}
}
};