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 (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and nrespectively.
从A数组的后面往前填充。省掉数组插入时的移动操作。
class Solution { public: void merge(int A[], int m, int B[], int n) { while (n) { A[m+n-1] = (!m || A[m-1]<B[n-1]) ? B[--n] : A[--m]; } } };
代码参考自:
https://leetcode.com/discuss/30859/1-line-solution
Merge Sorted Array -- leetcode
原文:http://blog.csdn.net/elton_xiao/article/details/45268641