描述:合并两个排序的整数数组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. 【Weex学习】环境搭建

    教程来源:http://jspang.com/2017/07/12/weex/,我本地是第一次安装Android Studio和教程有些出入 一.软件安装 1.安装Node.js 2.安装Java(h ...

  2. October 15th 2017 Week 42nd Sunday

    Excellence is a continuous process and not an accident. 卓越是一个持续的过程而不是一个偶然事件. It is said that ten tho ...

  3. Mojave使用pyenv安装python-zlib错误

    mojave使用pyenv编译python出现 zipimport.ZipImportError: can't decompress data; zlib not available错误 解决方案: ...

  4. 团队作业——Alpha冲刺 5/12

    团队作业--Alpha冲刺 冲刺任务安排 杨光海天 今日任务:编辑界面完成部分内容,学习了下拉菜单控件的建立,完善界面标题内容,以及交互. 明日任务:继续完善编辑界面,学习使用gallery,着手配图 ...

  5. DOM操作XML文件

    一.IE中的XML(IE低版本才支持) 在统一的正式规范出来以前,浏览器对于 XML 的解决方案各不相同.DOM2 级提出了动态创建 XML DOM 规范,DOM3 进一步增强了 XML DOM. 所 ...

  6. C结构体数组赋值

    #include <stdio.h> #include <stdlib.h> struct MyStruct { int a; char b; }; struct MyStru ...

  7. python第三十四课——1.匿名函数的定义和使用

    演示匿名函数的定义和使用 # 定义无参有返回值的有名函数: def func(): return True # 定义无参有返回值的匿名函数 f=lambda : True # 调用有名函数执行 pri ...

  8. php 数据集转换树、递归重组节点信息多维数组(转)

    一.将数据集转换成树 /** * 将返回的数据集转换成树 * @param array $list 数据集 * @param string $pk 主键 * @param string $pid 父节 ...

  9. Jenkins RCE(CVE-2018-1000861)

    先说通过IDEA利用JPDA远程调试tomcat程序 在catalina.sh添加,或者catalina.bat内容不动用如下命令开启,默认是开启8000端口 set JAVA_OPTS=-Xdebu ...

  10. VC++环境下单文档SDI与OpenGL多视图分割窗口的实现-类似3DMAX的主界面

    本文主要讲述如何在VC++环境下实现单文档SDI与OpenGL多视图分割窗口,最终的界面类似3DMAX的主界面.首先给出我实现的效果图: 整个实现过程网络上有很多零散的博文,请各位自行搜索,在基于对话 ...