Merge two given sorted integer array A and B into a new sorted integer array.

Example
A=[1,2,3,4] B=[2,4,5,6] return [1,2,2,3,4,4,5,6] Challenge
How can you optimize your algorithm
if one array is very large and the other is very small?

此题要求返回新数组。由于可以生成新数组,故使用常规思路按顺序遍历即可。

C++:

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) {
if (A.empty()) return B;
if (B.empty()) return A; int aLen = A.size(), bLen = B.size();
vector<int> C;
int i = , j = ;
while (i < aLen && j < bLen) {
if (A[i] < B[j]) {
C.push_back(A[i]);
++i;
} else {
C.push_back(B[j]);
++j;
}
} // A has elements left
while (i < aLen) {
C.push_back(A[i]);
++i;
} // B has elements left
while (j < bLen) {
C.push_back(B[j]);
++j;
} return C;
}
};

JAVA:

class Solution {
/**
* @param A and B: sorted integer array A and B.
* @return: A new sorted integer array
*/
public ArrayList<Integer> mergeSortedArray(ArrayList<Integer> A, ArrayList<Integer> B) {
if (A == null || A.isEmpty()) return B;
if (B == null || B.isEmpty()) return A; ArrayList<Integer> C = new ArrayList<Integer>();
int aLen = A.size(), bLen = B.size();
int i = 0, j = 0;
while (i < aLen && j < bLen) {
if (A.get(i) < B.get(j)) {
C.add(A.get(i));
i++;
} else {
C.add(B.get(j));
j++;
}
} // A has elements left
while (i < aLen) {
C.add(A.get(i));
i++;
} // B has elements left
while (j < bLen) {
C.add(B.get(j));
j++;
} return C;
}
}

源码分析

分三步走,后面分别单独处理剩余的元素。

复杂度分析

遍历 A, B 数组各一次,时间复杂度 O(n), 空间复杂度 O(1).

Challenge

两个倒排列表,一个特别大,一个特别小,如何 Merge?此时应该考虑用一个二分法插入小的,即内存拷贝。

Merge Sorted Array II的更多相关文章

  1. Lintcode: Merge Sorted Array II

    Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B= ...

  2. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  3. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  4. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  5. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  6. 43. Merge Sorted Array && LRU Cache

    Merge Sorted Array OJ: https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer ...

  7. 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  8. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. [OJ] Find Minimum in Rotated Sorted Array II

    LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...

随机推荐

  1. linux-常用命令备注

    //杀掉某个进程-xargs应用 ps aux | grep "udplog.js" | cut -c 9-15 | xargs kill -9 //远程拷贝文件或文件夹 sudo ...

  2. DPF.Android.Native.Components.v2.8.1 for delphi xe6 使用DPFJAlertDialog遇到的问题

    使用DPFJAlertDialog控件时发现DPFJAlertDialog1Click不能捕获到对话框到底按了那个按键,上网搜索后找到了解决方法: 打开DPF.Android.JAlertDialog ...

  3. screen工具

    1.背景 系统管理员经常需要SSH 或者telent 远程登录到Linux 服务器,经常运行一些需要很长时间才能完成的任务,比如系统备份.ftp 传输等等.通常情况下我们都是为每一个这样的任务开一个远 ...

  4. asp.net 类,接口

    ASP.NET抽象类和接口的比较 相同点 ●都不能被直接实例化,都可以通过继承实现其抽象方法. ●都是面向抽象编程的技术基础,实现了诸多的设计模式. 不同点 ●接口支持多继承:抽象类不能实现多继承.  ...

  5. css css预处理器

    CSS预处理器(css preprocessor) 1.less: 2.sass: 3.scss: 4.stylus 参考: http://hao.jser.com/archive/2507/ htt ...

  6. c++缓冲区------c++ Primer Plus

    通常,通过使用缓冲区可以更高效地处理输入和输出.缓冲区是用作中介的内存块,它是将信息从设备传输到程序或从程序传输给设备的临时存储工具.通常,像硬盘驱动器这样的设备以512字节(或更多)的块为单位来传输 ...

  7. Socket 简易静态服务器 WPF MVVM模式(一)

    整体代码下载 主要实现功能: Socket的简单应用 可修改IP和端口 显示来访信息 界面设计: 界面采用MVVM设计,很简陋. 前台的主要目的是 输入IP地址 输入端口 输入文件目录 开启监听和停止 ...

  8. JSON Path表达式

      JSON Path 描述 $ 表示根元素 @ 表示当前节点 .  表示子节点 .. 选择所有符合条件的节点 * 所有节点 [] 迭代器标识,如数组下标 [,] 支持迭代器中多选 [start:en ...

  9. 6w6:第六周程序填空题3

    描述 下面的程序输出结果是: A::Fun A::Do A::Fun C::Do 请填空: #include <iostream> using namespace std; class A ...

  10. DRF中的版本控制

    一.为什么要有版本 某些客户端 使用低版本只维护不开发新功能 v1 主要的产品还要不断的更新迭代功能 v2 API 版本控制允许我们在不同的客户端之间更改行为(同一个接口的不同版本会返回不同的数据). ...