题目链接https://leetcode.com/problems/merge-sorted-array/
这道题是merge sort中merge的过程,有一个trick是从后往前merge,这样可以避免多余的数组操作
class Solution { public: void merge(int A[], int m, int B[], int n) { int i = m - 1, j = n - 1; int len = m + n - 1; while(i >= 0 && j >= 0) { if(A[i] > B[j]) { A[len] = A[i]; i--; } else { A[len] = B[j]; j--; } len--; } while(i >= 0) { A[len] = A[i]; i--; len--; } while(j >= 0) { A[len] = B[j]; j--; len--; } } };