Given two sorted integer arrays A and B, merge B into A as one sorted array.
思路:
因为A的后面的部分都是空的留出来给我们放元素,所以最好是从后往前塞元素进去
void mergeSortedArray(int A[], int m, int B[], int n) { // write your code here int i = m-1; int j = n-1; int index = m+n-1; while(i>=0&&j>=0){ if(A[i]>=B[j]){ A[index]=A[i]; index--; i--; }else{ A[index]=B[j]; index--; j--; } } while(i>=0){ A[index--]=A[i--]; } while(j>=0){ A[index--]=B[j--]; } }