[LintCode] 合并排序数组II

 1 class Solution {
 2 public:
 3     /**
 4      * @param A: sorted integer array A which has m elements, 
 5      *           but size of A is m+n
 6      * @param B: sorted integer array B which has n elements
 7      * @return: void
 8      */
 9     void mergeSortedArray(int A[], int m, int B[], int n) {
10         // write your code here
11         int pa = m - 1, pb = n - 1, p = m + n - 1;
12         while (pa >= 0 && pb >= 0) {
13             if (A[pa] >= B[pb]) A[p--] = A[pa--];
14             else A[p--] = B[pb--];
15         }
16         while (pb >= 0) A[p--] = B[pb--];
17     }
18 };

 

posted @ 2015-06-29 22:57  jianchao-li  阅读(438)  评论(0编辑  收藏  举报