lintcode:合并排序数组
题目:
合并两个排序的整数数组A和B变成一个新的数组。
样例
给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]
挑战
你能否优化你的算法,如果其中一个数组很大而另一个数组很小?
解题:
利用Java的ArrayList很简单的,时间复杂度O(n+m)两个数组都要遍历一遍,对应两个数组长度差别很大的情况,效率就低了
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) {
// write your code here
ArrayList merge = new ArrayList();
int aSize = A.size();
int bSize = B.size();
int i=0;
int j = 0;
while(i<aSize && j<bSize){
if(A.get(i)<=B.get(j)){
merge.add(A.get(i));
i++;
}else{
merge.add(B.get(j));
j++;
}
}
while(i<aSize){
merge.add(A.get(i));
i++;
}
while(j<bSize){
merge.add(B.get(j));
j++;
}
return merge;
}
}
总耗时: 1832 ms
这个好无节操的哦
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
int bSize = B.size();
for(int i=0;i<bSize;i++)
A.add(B.get(i));
Collections.sort(A);
return A;
}
}
总耗时: 1340 ms
Python程序:
Python也可以无节操的来
class Solution:
#@param A and B: sorted integer array A and B.
#@return: A new sorted integer array
def mergeSortedArray(self, A, B):
# write your code here
A = A + B
A.sort()
return A
总耗时: 454 ms
当然也可以复杂的来了
class Solution:
#@param A and B: sorted integer array A and B.
#@return: A new sorted integer array
def mergeSortedArray(self, A, B):
# write your code here
C = []
aLen = len(A)
bLen = len(B)
i = 0
j = 0
while i<aLen or j <bLen:
if (i<aLen and j<bLen):
if A[i] <= B[j] :
C.append(A[i])
i+=1
else:
C.append(B[j])
j+=1
if i==aLen and j<bLen :
C.append(B[j])
j+=1
if i<aLen and j==bLen:
C.append(A[i])
i+=1 return C
总耗时: 314 ms
lintcode:合并排序数组的更多相关文章
- lintcode:合并排序数组 II
题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] ...
- 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之后,然后 ...
- [LintCode] 合并排序数组II
class Solution { public: /** * @param A: sorted integer array A which has m elements, * but size of ...
- [LintCode] 合并排序数组
A subroutine of merge sort. class Solution { public: /** * @param A and B: sorted integer array A an ...
- LintCode之合并排序数组II
题目描述: 分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的. 我的代码: public class Solution { /* * @ ...
- lintcode: 把排序数组转换为高度最小的二叉搜索树
题目: 把排序数组转换为高度最小的二叉搜索树 给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树. 样例 给出数组 [1,2,3,4,5,6,7], 返回 4 / \ 2 6 / \ / ...
- LintCode之合并排序数组
题目描述: 我的代码: public class Solution { /* * @param A: sorted integer array A * @param B: sorted integer ...
- [LintCode笔记了解一下]64.合并排序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. 思路: 因为A的后面的部分都是空的留出来给我们 ...
- 64. 合并排序数组.md
描述 合并两个排序的整数数组A和B变成一个新的数组. 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 您在真实的面试中是否遇到过这个题? 样例 给出 A = [1, 2, ...
随机推荐
- PHP流程控制语句下
ok,继续搞. while循环: while(expr){ 程序块: } 条件expr成立执行程序块,否则结束. do while循环:do while循环比while循环要多循环一次,它要先执行程序 ...
- c#中的结构与枚举
结构 与c++不同的是,结构应该定义在命名空间或者类里面,成员变量叫字段,字段并且有访问控制符,每个字段前要加一个下划线 例子 using System; using System.Collectio ...
- CentOS 7.0安装MPlayer
自带的播放器不管rmvb还是mp4都不能播放,也搜索不到解码器. CentOS7epel装不上所以也没有rpmfusion,所以决定自己编译mplayer 首先是要获取源代码. 首先是主程序的源代码. ...
- 纯JS文本比较工具
前段时间由于工作需要写了一个纯JS文本比较工具 在这里与大家分享下 算法有待优化,还希望大家多多指教 先上效果图: 奉上源码(把源码保存为html格式的文件就可以直接运行了): <!doctyp ...
- ASP.NET MVC +EasyUI 权限设计(三)基础模块
请注明转载地址:http://www.cnblogs.com/arhat 在上一章中呢,我们基本上搭建好了环境,那么本章我们就从基础模块开始写起.由于用户,角色,动作三个当中,都是依赖与动作的,所以本 ...
- LintCode-Word Segmentation
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- IIS搭建本地服务器,花生壳实现外网通过域名访问网站
配置服务器 作为一个青年,没有实力,做不出标图所示的服务器. 作为一个学生,买不起服务器 作为一个小孩,买不起域名 但别忘了 作为一个平民玩家,只要有耐心 装备迟早会做出来的 (注:感觉有钱与没钱还是 ...
- AngularJs遇到的小坑与技巧
1. templateURL和路由之类的要在web server下运行. 2. 使用模板replace设为true,模板里也要有相应的标签,否则不出现任何数据. 3. 1.2版本之后,ngRoute模 ...
- SHELL syntax error:unexpected end of file 提示错误
SHELL syntax error:unexpected end of file 提示错误 if [ -n "$1" ] then " else " fi e ...
- HTML特殊转义字符对照表
字符 十进制 转义字符 字符 十进制 转义字符 字符 十进制 转义字符 ? ¡ ¡ Á Á Á á á á ¢ ¢ ¢ Â Â ˆ â â £ £ £ Ã Ã Ã ã ã ã ¤ ¤ ¤ Ä Ä &a ...