Merge two given sorted integer array A and B into a new sorted integer array.

Example
A=[1,2,3,4] B=[2,4,5,6] return [1,2,2,3,4,4,5,6] Challenge
How can you optimize your algorithm if one array is very large and the other is very small?

没什么好说的,Arraylist来做merge, 建一个新ArrayList

 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
if (A==null || A.size()==0) return B;
if (B==null || B.size()==0) return A;
ArrayList<Integer> res = new ArrayList<Integer>();
int i=0, j=0;
for (int k=0; k<A.size()+B.size(); k++) {
if (i<A.size() && j<B.size() && A.get(i) < B.get(j)) {
res.add(A.get(i));
i++;
}
else if (i<A.size() && j<B.size() && A.get(i) >= B.get(j)){
res.add(B.get(j));
j++;
}
else if (i<A.size()) {
res.add(A.get(i));
i++;
}
else {
res.add(B.get(j));
j++;
}
}
return res;
}
}

Lintcode: Merge Sorted Array II的更多相关文章

  1. Merge Sorted Array II

    Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B= ...

  2. LintCode Merge Sorted Array

    两个排好序的数组, A有m个数, 长度够长, B有n个数, 长度为n, 将B放到A里, 不用buffer数组(临时数组), 就用A将两个合在一起, 同时排好序. 方法: 从右边将两个数组里最大的数取出 ...

  3. [OJ] Find Minimum in Rotated Sorted Array II

    LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...

  4. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  5. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  6. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  7. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  8. 43. Merge Sorted Array && LRU Cache

    Merge Sorted Array OJ: https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer ...

  9. 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

随机推荐

  1. Partitioning

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The simplest scheme f ...

  2. locations in main memory to be referenced by descriptive names rather than by numeric addresses

    Computer Science An Overview _J. Glenn Brookshear _11th Edition Chapter 6 Programming Languages As s ...

  3. kudu

    Kudu White Paper http://www.cloudera.com/documentation/betas/kudu/0-5-0/topics/kudu_resources.html h ...

  4. CentOS 6.5 源码安装MySQL5.6.26

    1:下载安装cmake (mysql5.5以后是通过cmake来编译的) 2:创建mysql的安装目录及数据库存放目录 #mkdir /usr/mysql                 //安装my ...

  5. ubuntu12.04 安装 php5.4/php5.5

    1:修改源(我使用163的源)直接修改/etc/apt/sources.list deb http://mirrors.163.com/ubuntu/ precise main universe re ...

  6. MyEclipse 使用快捷键

    将光标移到文档末尾,ctrl+end将光标移到文档首,ctrl+home行首,home行尾,end.

  7. Linux core 文件介绍

    Linux core 文件介绍 http://www.cnblogs.com/dongzhiquan/archive/2012/01/20/2328355.html 1. core文件的简单介绍在一个 ...

  8. jfinal

    http://blog.csdn.net/zb0567/article/details/21083021

  9. Magento的迁移方法

    Magento有很多配置内容,比如说CMS配置页.Static Stock.多语言配置等等,所以做数据迁移很有必要性,下面就说说如何做迁移 这个技术文章是从网上整理的,不过一个很重要的点被疏忽了,我在 ...

  10. empty()函数经典详解

    <?php /** * 当var不存在,返回TRUE; * 当var存在,并且是一个非空非零的值(真值)时返回 FALSE 否则返回 TRUE . * 以下的东西被认为是空的: * * 1.&q ...