题目:

合并排序数组

合并两个排序的整数数组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:合并排序数组的更多相关文章

  1. lintcode:合并排序数组 II

    题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] ...

  2. 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之后,然后 ...

  3. [LintCode] 合并排序数组II

    class Solution { public: /** * @param A: sorted integer array A which has m elements, * but size of ...

  4. [LintCode] 合并排序数组

    A subroutine of merge sort. class Solution { public: /** * @param A and B: sorted integer array A an ...

  5. LintCode之合并排序数组II

    题目描述: 分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的. 我的代码: public class Solution { /* * @ ...

  6. lintcode: 把排序数组转换为高度最小的二叉搜索树

    题目: 把排序数组转换为高度最小的二叉搜索树 给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树. 样例 给出数组 [1,2,3,4,5,6,7], 返回 4 / \ 2 6 / \ / ...

  7. LintCode之合并排序数组

    题目描述: 我的代码: public class Solution { /* * @param A: sorted integer array A * @param B: sorted integer ...

  8. [LintCode笔记了解一下]64.合并排序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. 思路: 因为A的后面的部分都是空的留出来给我们 ...

  9. 64. 合并排序数组.md

    描述 合并两个排序的整数数组A和B变成一个新的数组. 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 您在真实的面试中是否遇到过这个题? 样例 给出 A = [1, 2, ...

随机推荐

  1. JS定时器实例解析

    在javascritp中,有两个关于定时器的专用函数. 分别为:1.倒计定时器:timename=setTimeout("function();",delaytime);2.循环定 ...

  2. css透明(支持各浏览器)

    opacity: 0.4;filter: progid:DXImageTransform.Microsoft.Alpha(opacity=40); -ms-filter: "progid:D ...

  3. python 数字、字符串、列表常用函数

    一.数字的标准类型: cmp():比较两个数的大小:返回值(-1,0,1). str():数字转化成字符串. type():返回数字类型. 转换工厂函数: int(obj,base=10) long( ...

  4. c#中的ref、out、params参数

    out参数 与c++的引用的对比 out参数可以用来传递方法返回值,与c++中的引用有点像,但是还有有些不同: - 调用方法的时候必须写out参数 - 调用方法之前必须先分配空间 - 调用方法之前不用 ...

  5. [转]system函数返回值探究

    对于system这个函数的功能早就有一定了解,读书期间,就学习了UNIX系统编程这本书,后来买了APUE.我这个人总是有好读书不求甚解的毛病.对于system函数只知其一,不知其二.后来被人问起相关的 ...

  6. 数据分析≠Hadoop+NoSQL,不妨先看完善现有技术的10条捷径(分享)

            Hadoop让大数据分析走向了大众化,然而它的部署仍需耗费大量的人力和物力.在直奔Hadoop之前,是否已经将现有技术推向极限?这里总结了对Hadoop投资前可以尝试的10个替代方案, ...

  7. linux下gcc编译的参数详细说明

    参考网址:1 http://hi.baidu.com/zengzhaonong/item/f1f9383565fa5c302e0f8125 gcc使用方法 汇总 2 http://s99f.blog. ...

  8. Wireshark 入门

    1.过滤目的地是百度的IP包. 百度的ip: 命令:ip.src eq 61.135.169.125 过滤ip来源是61.135.169.125 ip.dst eq 61.135.169.125 过滤 ...

  9. Cocos2dx中的四种控件及主要用法

    1.控件:即控制对象,控制按钮之类的精灵 2.主要介绍四大类控件: CCControlSlider:进度条 CCControlSwitch:开关 CCScale9Sprite:9妹图(用于缩放) CC ...

  10. ASP.NET Web – 状态管理

    状态类型 客户端或服务器资源 有效时间 ViewState 客户端 只在一个页面中 Cookie 客户端 关闭浏览器时会删除临时cookie,永久cookie存储在客户系统的磁盘上 Session 服 ...