题目:
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 respectively.
看到讨论区里一位高手写的
while(n>0){
if(m <=0 || A[m-1] < B[n-1])
A[n+m-1] = B[--n];
else
A[n+m-1] = A[--m];
}
}
另一个版本:
void merge(int A[], int m, int B[], int n) {
while(n>0){
if(m>0 && A[m-1]>B[n-1]){
A[n+m-1]=A[m-1];
m--;
}else{
A[n+m-1]=B[n-1];
n--;
}
}
}