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. MySQL出现大量unauthenticated user的问题

    发现这算属MySQL的一个bug,不管连接是通过hosts还是ip的方式,MySQL都会对DNS做反查,IP到DNS,由于反查的接续速度过 慢(不管是不是isp提供的dns服务器的问题或者其他原因), ...

  2. P85练习3

    public class P85Excise { public static void main(String[] args) { // TODO 自动生成的方法存根 int i =1; float ...

  3. General protection fault Exceptions in Linux/IA32 Systems

    Computer Systems A Programmer's Perspective Second Edition Exception number Description Exception cl ...

  4. Shared libraries

    Computer Systems A Programmer's Perspective Second Edition Shared libraries are modern innovations t ...

  5. 迷宫dfs

    #include<stdio.h>int mov1[4]={0,0,1,-1};int mov2[4]={1,-1,0,0};int map[5][5]={0,1,0,0,1,      ...

  6. (转)Linux下MatlabCompilerRuntime的安装和使用

    1MCR简介 MCR之前是 Matlab Component Runtime的缩写,后更名为Matlab Compiler Runtime.MCR实际上是一组独立的共享库,也即是常说的动态连接库,所起 ...

  7. NRF51822之GPIOTE使用

    ---恢复内容开始--- 在上篇介绍nrf51822的GPIOTE http://www.cnblogs.com/libra13179/p/5336580.html 我们现在开始下水游泳. /** @ ...

  8. 常用ARM汇编指令

    常用ARM汇编指令 [日期:2012-07-14] 来源:Linux社区  作者:xuyuanfan77 [字体:大 中 小]     在嵌入式开发中,汇编程序常常用于非常关键的地方,比如系统启动时初 ...

  9. php--递归调用

  10. js严格模式“use strict”

    js的严格模式会放弃js中的一些不正规的写法,参考 http://www.cnblogs.com/God-Shell/p/3139329.html: 使用声明"use  strict&quo ...