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,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的更多相关文章
- 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= ...
- [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 ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 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 ...
- 43. Merge Sorted Array && LRU Cache
Merge Sorted Array OJ: https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer ...
- 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 ...
- 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 ...
- [OJ] Find Minimum in Rotated Sorted Array II
LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...
随机推荐
- (转)Linux环境进程间通信系列(五):共享内存
原文地址:http://www.cppblog.com/mydriverc/articles/29741.html 共享内存可以说是最有用的进程间通信方式,也是最快的 IPC 形式.两个不同进程 A ...
- HBuilder生成代码行快捷键
1.在body内输入div.abc按下tab键 效果:<div class="abc"></div> 2.在body内输入div#abc按下tab键 效果: ...
- DB2 函数快速构造测试数据
函数快速构造测试数据 [案例]使用DB2内置函数快速构造测试数据 无论您是在用原型证明某一概念,还是开发一个全新的应用程序,或者只是学习 SQL,您都需要在您的应用程序上运行测试数据.为了有效地测试应 ...
- [other] 代码量代码复杂度统计-lizard
[other] 代码量代码复杂度统计-lizard lizard的可以用来统计下面的一些数据 不包含代码注释的代码行数 CCN 代码的复杂度,也就是分支复杂度 token的个数(关键字,标示符,常量, ...
- windows service使用log4net 记录日志
最近写了个定时邮件推送的服务,当利用lognet4记录日志时,发现日志并没有记录.后来明白windows 服务一般默认是在C:\Windows\System 或是C:\Windows\System32 ...
- .Net Core 项目部署IIS简单步骤
1.新建一个解决方案: 我习惯会把运行文件移至一级目录 然后清除CoreTest 文件夹里面的文件 2.在解决方案中新建一个项目 点击确认有,这里有几种选择类型,我一般选择空类型(这里需要注意一下,空 ...
- javascript framework vue.js
vue.js 参考: http://cn.vuejs.org/guide/installation.html 不管使用何框架,首先都是要创建它的实例: var vue = new Vue({//参 ...
- 「SDOI2017」新生舞会
题目链接 戳我 \(Describe\) 有一场舞会,n个男生,n个女生,要组成n对舞伴,男生i和女生j组队的适合度是\(a_{ij}\), 不适合度是\(b_{ij}\), 让你求\(max(\su ...
- python merge、concat合并数据集
数据规整化:合并.清理.过滤 pandas和python标准库提供了一整套高级.灵活的.高效的核心函数和算法将数据规整化为你想要的形式! 本篇博客主要介绍: 合并数据集:.merge()..conca ...
- List_Delete
/*Sorting from little to large use List*/ #include <stdio.h> /* printf, scanf, NULL */ #includ ...