题目描述:

我的代码:

 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. v-if指令

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. cannot open shared object file: No such file or directory解决

    cannot open shared object file: No such file or directory解决   ./move_db: error while loading shared ...

  3. 【小刘的linux学习笔记 】——01认识操作系统

    1.操作系统的地位 计算机系统由硬件和软件两部分组成.通常把未配置软件的计算机称为裸机.直接使用裸机不仅不方便,而且将严重降低工作效率和机器的利用率. 操作系统(OS,Operation System ...

  4. Oracle-常见的错误

    1.见下面的例子 create or replace procedure p_qr_stu_cid(s_id in number, c_id out number) as begin select t ...

  5. python之从文件中按行读取数据

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'jiangwenwen' # 从文件中按行读取数据 file = open(& ...

  6. webpack的code spliting与chunks

    webpack的code spliting与chunks :https://blog.csdn.net/liuqi332922337/article/details/53020992

  7. Codeforces 1105D (BFS)

    题面 传送门 分析 考虑BFS while(棋盘没有满){ for 玩家 p{ 对p进行BFS,走s[p]步 } } 对于每个玩家p BFS的时候如果到了格子(x,y),就把\(vis[x][y]\) ...

  8. python学习二十一天文件可读,可写,可执行的操作

    文件无非是可读,可写,可执行的操作,分别对应的模式 r ,w,x,只读模式,只写模式,只执行模式,a模式为追加模式,实际也是写操作模式,r+,w+,a+ 可读写模式,下面详细说模式的用法 1,文件的模 ...

  9. spring 配置属性细节

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/qilixiang012/article/details/28233811 概要(红色为上一篇所讲,蓝 ...

  10. Mysql一些概念,基本没啥用,

    关系型数据库管理系统(RDBMS):是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据.特点:1.数据以表格的形式出现2.每行为各种记录名称3.每列为记录名称所对应的数 ...