码迷,mamicode.com
首页 > 编程语言 > 详细

[LintCode] 合并排序数组

时间:2015-06-29 19:23:28      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:

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 };

 

[LintCode] 合并排序数组

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4607835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!