A subroutine of merge sort.

 class Solution {
public:
/**
* @param A and B: sorted integer array A and B.
* @return: A new sorted integer array
*/
vector<int> mergeSortedArray(vector<int> &A, vector<int> &B) {
// write your code here
vector<int> merge;
int pa = , pb = ;
while (pa < (int)A.size() && pb < (int)B.size()) {
if (A[pa] <= B[pb]) merge.push_back(A[pa++]);
else merge.push_back(B[pb++]);
}
while (pa < (int)A.size())
merge.push_back(A[pa++]);
while (pb < (int)B.size())
merge.push_back(B[pb++]);
return merge;
}
};

[LintCode] 合并排序数组的更多相关文章

  1. lintcode:合并排序数组 II

    题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] ...

  2. LintCode——合并排序数组II

    描述:合并两个排序的整数数组A和B变成一个新的数组 样例:给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 1.Python:先将数组B加到数组A之后,然后 ...

  3. [LintCode] 合并排序数组II

    class Solution { public: /** * @param A: sorted integer array A which has m elements, * but size of ...

  4. lintcode:合并排序数组

    题目: 合并排序数组 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 挑战 你能否优化你的算法,如果 ...

  5. LintCode之合并排序数组II

    题目描述: 分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的. 我的代码: public class Solution { /* * @ ...

  6. lintcode: 把排序数组转换为高度最小的二叉搜索树

    题目: 把排序数组转换为高度最小的二叉搜索树 给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树. 样例 给出数组 [1,2,3,4,5,6,7], 返回 4 / \ 2 6 / \ / ...

  7. LintCode之合并排序数组

    题目描述: 我的代码: public class Solution { /* * @param A: sorted integer array A * @param B: sorted integer ...

  8. [LintCode笔记了解一下]64.合并排序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. 思路: 因为A的后面的部分都是空的留出来给我们 ...

  9. 64. 合并排序数组.md

    描述 合并两个排序的整数数组A和B变成一个新的数组. 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 您在真实的面试中是否遇到过这个题? 样例 给出 A = [1, 2, ...

随机推荐

  1. 【Android】Architecture Components最佳实践--Lifecycles

    UI controllers (activities and fragments) 中代码越少越好,不应该自己去请求数据,而是用ViewModel来更新数据,并且监听LiveData来更新UI UI ...

  2. FTP服务搭建与配置

    FTP介绍 大企业用的基本都是自动化发布工具,会用GIT企业发布的版本上传到服务器, 使用vsftpd搭建ftp服务(上) http://blog.csdn.net/qq_26941173/artic ...

  3. python-求直角三角形斜边

    设计一个求直角三角形斜边长的函数(两条直角边为参数,求最长边) 如果直角边边长分分别为3和4,那么返回的结果应该像这样: The right triangle third side's length ...

  4. winfrom更新

    原理: 工具生成更新配置节xml放到文件服务器上,外网可访问: 能过本地配置文件与服务器配置文件日期属性对比及配置节版本与大小属性判断有无更新: 存在更新,将文件从服务器下载到客户端,并替换原程序重启 ...

  5. Atitit.  Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    Atitit.  Exception in thread "main" java.lang.Error: Unresolved compilation problem: 1.1. ...

  6. 第一篇:初识python

    1.python介绍 Python, 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum于1989年发明,第一个公开发行版发行于1991年. Python的官方介绍是: ...

  7. java web 打水印

    /** * 把图片印刷到图片上 * * @param pressImg -- * 水印文件 * @param targetinp -- * 目标文件 * @param x * --x坐标 * @par ...

  8. js将秒数换算成时分秒

    转载自:http://jingyan.baidu.com/article/375c8e19a0413925f2a229d2.html <script language="javascr ...

  9. Unix系统编程(四)creat系统调用

    我好疑惑啊,creat系统调用为啥没有以e结尾呢?搞得我每次都怀疑我敲错了. 在早期的UNIX实现中,open只有两个参数,无法创建新文件,而是使用creat系统调用创建并打开一个新文件. int c ...

  10. Xampp + Zend Studio + xDebug 环境搭建 (Mac,Windows都适用)

    这几天折腾了一下PHP开发环境的搭建,现总结一下安装步骤: 1. 安装 Zend Studio,然后破解. 2. 安装 Xampp  3. 配置 Xampp      3.1 配置 Apache服务端 ...