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 ...
随机推荐
- 整合Office Web Apps至自己的开发系统
原文出处:http://www.cnblogs.com/poissonnotes/p/3267190.html 还可参考:https://www.cnblogs.com/majiang/p/36729 ...
- Eclipse下Android的NDK开发环境配置
编辑2016年7月26日——增加了下载网址,修改了一些错误. 摸索了一周,走了很多弯路,磕磕绊绊,总算是弄好了NDK的开发环境,在这里总结一下吧. 一.Android NDK开发环境 首先下载安装JR ...
- 请教一个Jquery ligerui 框架的小问题
关闭子窗体时,要刷新父窗体,百度了很多像使用“window.opener.location.reload();”都不行,和easyui框架是有区别的 在子窗体里写Response.Write(&quo ...
- 四则运算(Java) 陈志海 邓宇
目录 Github项目地址 PSP表格 功能要求 题目 功能(已全部实现) 效能分析 设计实现过程 数值生成 算式生成 问题集生成 设计实现过程 代码说明 测试运行 代码覆盖率 项目小结 Github ...
- mysql数据库学习小结
数据库的学习可以从以下几个层次了解掌握,这样思路清晰后后面不管怎么变化都可以随时应变: 1.mysql基础知识 2.操作数据库的方法,增 删 改 查 3.jdbc连接数据库,工作原理 难点重点,如:P ...
- C#中base的作用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 创建Database Diagrams时遇到的问题
SQL2008 R2中时,Diagrams的问题 Error: ------------------------------ Database diagram support objects cann ...
- L - Ch’s gift HDU - 6162
Ch’s gift Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- kali linux之拒绝服务
Dos不是DOS(利用程序漏洞或一对一资源耗尽的denial of service拒绝服务) DDoS分布式拒绝服务(多对一的攻击汇聚资源能力,重点在于量大,属于资源耗尽型) 历史 以前:欠缺技术能力 ...
- weblogic启动一闪而过
点击startWebLogic.cmd的时候,一闪而过 我的原因:JAVA_HOME中的路径是不能带有空格:我的电脑是64位的,jdk(32位)安装路径默认带有空格还有括号,所以重新装jdk,装在没有 ...