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,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?
没什么好说的,Arraylist来做merge, 建一个新ArrayList
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) {
// write your code here
if (A==null || A.size()==0) return B;
if (B==null || B.size()==0) return A;
ArrayList<Integer> res = new ArrayList<Integer>();
int i=0, j=0;
for (int k=0; k<A.size()+B.size(); k++) {
if (i<A.size() && j<B.size() && A.get(i) < B.get(j)) {
res.add(A.get(i));
i++;
}
else if (i<A.size() && j<B.size() && A.get(i) >= B.get(j)){
res.add(B.get(j));
j++;
}
else if (i<A.size()) {
res.add(A.get(i));
i++;
}
else {
res.add(B.get(j));
j++;
}
}
return res;
}
}
Lintcode: Merge Sorted Array II的更多相关文章
- 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= ...
- LintCode Merge Sorted Array
两个排好序的数组, A有m个数, 长度够长, B有n个数, 长度为n, 将B放到A里, 不用buffer数组(临时数组), 就用A将两个合在一起, 同时排好序. 方法: 从右边将两个数组里最大的数取出 ...
- [OJ] Find Minimum in Rotated Sorted Array II
LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...
- [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 ...
随机推荐
- Delphi格式化输出函数(1): Format
vars: string;begin//指令类型 types := Format('最大整数是: %d; 最小整数是: %d',[MaxInt,Low(Integer)]);//返回: 最大整数是: ...
- css 鼠标移动到按钮图片改变;图片换层;鼠标放上透明度改变直到隐藏;
css 鼠标移动到按钮图片改变: 方法一: <style> .pp a { width:575px; height:157px; background:url(1.jpg);/*图片地址* ...
- 64 位win 7或windows 8下的visual studio不能连接Oracle数据库调试网站的问题
在64 位win 7或windows 8系统下,visual studio直接F5运行网站调试,你会发现不能连接Oracle数据库,会报一个“ORA-06413: Connection not ope ...
- javaScript中的单引号与双引号
javaScript中的单引号与双引号没有什么区别.但因为xhtml规范要求所有xhtml属性要用双引号括起来.所以在javaScript中使用单引号. var html = '<h2 clas ...
- 比较setImmediate(func),setTimeout(func),process.nextTick(func)
node中的事件优先级机制: console.log('第一笔!'); process.nextTick(function() { console.log('吃个饭吧!'); setImmediata ...
- Housse Robber II | leetcode
可以复用house robber的代码,两趟dp作为两种情况考虑,选最大值 #include <stdio.h> #define MAX 1000 #define max(a,b) ( ( ...
- 去除Html标签
public static string ParseTags(string Htmlstring) { //删除脚本 Htmlstring = Regex.R ...
- for循环数据节点
1.需要实现的功能,动态填充多条银行卡信息 2.dom结构 3.数据节点 4.实现方式 //获取银行卡基本信息 CmnAjax.PostData("Handler/Users/Users.a ...
- 【python】RGB图片到灰度图的转换
在做立体匹配求深度图的时候遇到这个问题,用惯了matlab的rgb2gray,倒是不熟悉python的实现,在网上找到了相关方案,记下来已作备用 RGB到灰度图转换公式: Y' = 0.299 R + ...
- LightOj1074 - Extended Traffic(SPFA最短路)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每个城市有一个拥堵值a[i],m条单向路u到v,从u到v所需时 ...