题目:

合并排序数组

合并两个排序的整数数组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. DTCMS列表页自定义参数。

    1.频道管理中,URL配置,增加一个参数person_id 2.在photo_list.html模板页中,添加以下代码 <!--C#代码--> <%csharp%> strin ...

  2. CSS各个浏览器Hack的写法

    Hack是针对不同的浏览器去写不同的CSS样式,从而让各浏览器能达到一致的渲染效果,那么针对不同的浏览器写不同的CSS CODE的过程,就叫CSS HACK,同时也叫写CSS Hack.然后将Hack ...

  3. 1096. Consecutive Factors (20)

    Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...

  4. WebDev.WebServer40.exe已停止工作

    今天写程序的遇到这个错误 错误的原因是代码中有死循环

  5. keyboard

    一 按键驱动程序的简单实现 下面是基于中断和消息的按键驱动程序,其工作原理是:当应用程序读取键值时,会调用按键驱动程序的read函数,而我们实现的read函数检测完读取长度后没有直接读取键值而是等待按 ...

  6. 如何调优JVM - 优化Java虚拟机(大全+实例)

    堆设置 -Xmx3550m:设置JVM最大堆内存 为3550M. -Xms3550m:设置JVM初始堆内存 为3550M.此值可以设置与-Xmx相同,以避免每次垃圾回收完成后JVM重新分配内存. -X ...

  7. PythonChallenge 1:恺撒密码的解码

    题目: 解题思路:根据图中的K→M,O→Q,E→G,我们可以发现K,O,E这三个字母都向后移动了2位.据说恺撒率先使用了这一加密方法,因此将其命名为恺撒密码.它的基本思想是:通过把字母移动一定的位数来 ...

  8. c++ 信号量

    最近写了个c++项目,发现 ctrl + c 退出程序可以出发析构,但kill pid,则不会触发析构,导致现场数据丢失. 解决方案:同时捕捉以下信号. signal(SIGINT, &完成命 ...

  9. C# IL DASM 使用

    IL DASM反编译工具 使用C#的猿人或多或少都会对微软的IL反编译工具(ildasm.exe)有所认识.我最早接触到这工具是公司同事使用他反编译exe程序,进行研读和修改.感觉他还是很强大. IL ...

  10. 【HDOJ】【2089】不要62

    数位DP cxlove基础数位DP第一题 用容斥把所有的不吉利数字去掉就得到吉利数字的数量= =(满足区间减法) //HDOJ 2089 #include<cmath> #include& ...