题目描述:

我的代码:

 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[] a = new int[A.length+B.length];
int p=0,q=0,count=0;
//当有一个数组全都重新排好序之后就退出循环
while(p<A.length && q<B.length) {
if(A[p] <= B[q]) {
a[count++] = A[p];
p++;
}else {
a[count++] = B[q];
q++;
}
}
if(p < A.length) {
for(int i=p; i<A.length; i++) {
a[count++] = A[i];
}
}
if(q < B.length) {
for(int i=q; i<B.length; i++) {
a[count++] = B[i];
}
}
return a;
}
}

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

  1. lintcode:合并排序数组

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

  2. LintCode之合并排序数组II

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

  3. lintcode:合并排序数组 II

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

  4. lintcode: 寻找旋转排序数组中的最小值

    寻找旋转排序数组中的最小值 假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2). 你需要找到其中最小的元素. 你可以假设数组中不存在重复的 ...

  5. lintcode:寻找旋转排序数组中的最小值 II

    寻找旋转排序数组中的最小值 II 假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2). 你需要找到其中最小的元素. 数组中可能存在重复的元 ...

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

    把排序数组转换为高度最小的二叉搜索树    描述 笔记 数据 评测 给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树. 注意事项 There may exist multiple val ...

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

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

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

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

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

随机推荐

  1. AtCoder ABC 140D Face Produces Unhappiness

    题目链接:https://atcoder.jp/contests/abc140/tasks/abc140_d 题目大意 有一对 N 个人, 用字符串 S 表示, S[i] 如果等于 'L' 说明这个人 ...

  2. Python 学习笔记21 CMD执行测试用例

    使用CMD命令执行测试用例 当我们在ride中设计好测试用例后,我们可以使用ride的界面工具来选择和运行测试用例. 系统也会提供比较好的报告和日志的浏览功能. 但是这样的自动化,毕竟是需要手工介入的 ...

  3. 箭头函数中的this

    箭头函数中的this 箭头函数根据外层(函数或者全局)作用域来决定this 这样this就像其他面向对象的语言,在哪里定义就指向哪里 function foo() { return (x) => ...

  4. RabbitMQ ——四种ExChange及完整示例

    RabbitMQ常用的Exchange Type有fanout.direct.topic.headers这四种,下面分别进行介绍. 这四种类的exchange分别有以下一些属性,分别是: name:名 ...

  5. while例子 求1到100的和

  6. MySQL06-- mysql索引

    目录 一.索引介绍 1.什么是索引 2.索引类型介绍 3.索引管理 5.索引操作 6.前缀索引 7.联合索引 8.创建索引总结: 一.索引介绍 1.什么是索引 1)索引就好比一本书的目录,它能让你更快 ...

  7. Linux 下源码安装ngnix

    版本说明: NGINX 版本1.12.0 pcre-8.40 zlib-1.2.11 openssl-1.1.0i   安装过程 # ./configure  --prefix=/usr/ngnix  ...

  8. bzoj1004 [HNOI2008]Cards Burnside 引理+背包

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1004 题解 直接 Burnside 引理就可以了. 要计算不动点的个数,那么对于一个长度为 \ ...

  9. Vue 滚动条动画

    <template> <div class="home-main"> <div class="progress-main"> ...

  10. python tkinter的Label

    from tkinter import * window=Tk() window.title("my first window") window.geometry("50 ...