[LintCode] 合并排序数组

A subroutine of merge sort.

 1 class Solution {
 2 public:
 3     /**
 4      * @param A and B: sorted integer array A and B.
 5      * @return: A new sorted integer array
 6      */
 7     vector<int> mergeSortedArray(vector<int> &A, vector<int> &B) {
 8         // write your code here
 9         vector<int> merge;
10         int pa = 0, pb = 0;
11         while (pa < (int)A.size() && pb < (int)B.size()) {
12             if (A[pa] <= B[pb]) merge.push_back(A[pa++]);
13             else merge.push_back(B[pb++]);
14         }
15         while (pa < (int)A.size())
16             merge.push_back(A[pa++]);
17         while (pb < (int)B.size())
18             merge.push_back(B[pb++]);
19         return merge;
20     }
21 };

 

你可能感兴趣的:(code)