描述:合并两个排序的整数数组A和B变成一个新的数组

样例:给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]

1、Python:先将数组B加到数组A之后,然后对新数组进行排序

 class Solution:
"""
@param A: sorted integer array A
@param B: sorted integer array B
@return: A new sorted integer array
"""
def mergeSortedArray(self, A, B):
# write your code here
C = A + B
C.sort()
return C

2、Java

 public class Solution {
/**
* @param A: sorted integer array A
* @param B: sorted integer array B
* @return: A new sorted integer array
*/
public int[] mergeSortedArray(int[] A, int[] B) {
// write your code here
int Size_A = A.length;
int Size_B = B.length;
int[] result = new int[Size_A + Size_B];
int i = 0,j = 0,k = 0;
while(i < Size_A && j < Size_B){
if(A[i] <= B[j]){
result[k++] = A[i++];
}
else{
result[k++] = B[j++];
}
}
while(i < Size_A){
result[k++] = A[i++];
}
while(j < Size_B){
result[k++] = B[j++];
}
return result;
}
}

LintCode——合并排序数组II的更多相关文章

  1. lintcode:合并排序数组 II

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

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

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

  3. LintCode之合并排序数组II

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

  4. 6. 合并排序数组 II

    6. Merge Two Sorted Arrays Description Merge two given sorted integer array A and B into a new sorte ...

  5. [容易]合并排序数组 II

    题目来源:http://www.lintcode.com/zh-cn/problem/merge-sorted-array/

  6. [LintCode] 合并排序数组

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

  7. lintcode 中等题:搜索旋转排序数组II

    题目 搜索旋转排序数组 II 跟进“搜索旋转排序数组”,假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中. 样例 给出[3, ...

  8. lintcode:合并排序数组

    题目: 合并排序数组 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 挑战 你能否优化你的算法,如果 ...

  9. lintcode-64-合并排序数组 II

    64-合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 注意事项 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 样例 给出 A = [1, 2, 3, ...

随机推荐

  1. python 函数闭包()

    闭包(closure) 当一个函数在内部定义函数,并且内部的函数应用外部函数的参数或者局部变量,当内部函数被当做返回值的时候,相关参数和变量保存在返回函数中,这种结果,叫闭包 example1: de ...

  2. [IDEA_4] IDEA 从 GitHub 上 pull 项目到本地

    0. 说明 通过参考的链接我们已经知道了怎么安装配置 Git  .GitHub ,如何使用 IDEA 将本地项目上传到 GitHub. 现在是学习怎么通过 IDEA 将项目从 GitHub pull ...

  3. 《面向对象程序设计》c++第六次作业___calculator SE

    c++第五次作业 Calculator SE 代码 PS:这次作业延迟了很久,人要是迷茫啊----唉------ 新增GUI界面,使用Qt creator编写,纯代码生成控件.写坐标. 感觉Qt cr ...

  4. [转载]ArcGIS SERVER 9.3如何清除REST缓存

    有时候,发布了一个服务后,但是点击服务后,不能显示出来 http://hostname/ArcGIS/rest/services/服务名称/MapServer 这时候,十有八九是因为REST缓存没有清 ...

  5. python第三十课--异常(with as操作)

    演示with...as...操作 path=r'kaifanglist1.txt' with open(path,'r',encoding='utf-8') as fr: print(fr.read( ...

  6. 关于$namespace$和重载运算符

    $namespace$ 还记得列队和天天爱跑步吗?记得当时写部分分写的非常艰难,一大原因就是部分分之间有很多重名的数组,而且大小还不一样大,经常写着写着就串了,而且$maxn$有一次提交时用错了直接全 ...

  7. Shallwe学长的模拟赛

    NOIP Simulated Test 这个名字一听就很高端. T1:sGCD:http://uoj.ac/problem/48 题意概述:给定一个长度为$n$的序列,求$sgcd(a_1,a_i)$ ...

  8. ubuntu16.04之sudo问题

    问题描述: 我通过useradd test创建了test用户,并通过mkdir test创建了该用户对应的目录,再通过chown -R test /home/test将该目录及其子目录权限授予给tes ...

  9. Invoking "cmake" failed报错处理

    运行$ pip install -U rosdep rosinstall_generator wstool rosinstall six vcstools运行完成后再重新编译

  10. JAVA框架Struts2 servlet API

    一:servlet API 1)完全解耦接口: 使用ActionContext类进行相关操作: package jd.com.actioncontex; import com.opensymphony ...