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. Andrew Ng机器学习公开课笔记–Independent Components Analysis

    网易公开课,第15课 notes,11 参考, PCA本质是旋转找到新的基(basis),即坐标轴,并且新的基的维数大大降低 ICA也是找到新的基,但是目的是完全不一样的,而且ICA是不会降维的 对于 ...

  2. cookie 操作

    //创建并赋值 重新赋值也是这样操作 document.cookie="userId=828"; document.cookie="userName=hulk" ...

  3. 12秒开机!ExpressCache SSD缓存加速

    SSD固态硬盘的读写速度比传统硬盘快了很多,读取速度能到300M/s 写入速度大约在80M/S 但SSD硬盘的价格也笔记机械硬盘高了很多,128G的固态硬盘淘宝价大概在800左右,想想现在随便一个软件 ...

  4. 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)

    设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...

  5. postgre去重复记录

    postgre去重复记录,主要用到row定位的一个系统表示 “ctid”,能查出纯净的不重复的记录,那要删掉重复值也就容易了,自己去折腾吧. 我所涉及的是得到不重复的记录,就一句话: select c ...

  6. Ngrok 内网穿透利器

    Ngrok是什么 Ngrok 是一个反向代理,通过在公共的端点和本地运行的 Web 服务器之间建立一个安全的通道.Ngrok 可捕获和分析所有通道上的流量,便于后期分析和重放 为什么要使用Ngrok ...

  7. Android Studio工具修理集

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 1.Common依赖项目找不到.因为主项目没有引进setting.gradle 2.从Eclipse ...

  8. node中定时器的“先进”用法

    var DSQ = setInterval(function(){ console.log('zzq'); },1000); setTimeout(function(){ clearInterval( ...

  9. php练习:每5个商品一排

    老板说: 我要一行5个商品,每个长得都不一样 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  10. backbone extend 源码分析

    var extend = function(protoProps, staticProps) { var parent = this; var child; if (protoProps && ...