Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.


题目标签:Array

  题目给了我们两个有序数组,让我们merge sort,而且nums1 有足够的空间让我们存放 两个array。既然是要放到nums1里,那么我们如果比较nums1 和nums2 的数字,如果是nums2 的数字小的话,就要把nums2 的数字存放进nums1,我们还要另外存放nums1的数字,这样就需要额外space了。所以,我们可以从nums1的end 开始遍历回start, 然后设两个pointers 分别指向 两个array的最后一个数字,这样可以比完大小后直接存放进nums1。注意最后还需要检查一下,nums2是否还有剩下的数字,有的话都需要存进nums1。

Java Solution:

Runtime beats 38.77%

完成日期:04/05/2017

关键词:Array

关键点:Merge sort;Two Pointers;从end遍历回start

 public class Solution
{
public void merge(int[] nums1, int m, int[] nums2, int n)
{
/* if m = 0, meaning nums1's elements are all done. Need one more while loop after this
to take care of left elements of nums2.
if n = 0, meaning nums2's elements are done, the rest of nums1's elements are in the
right place. No need to take care of them.
*/
while(m > 0 && n >0)
{
if(nums1[m-1] > nums2[n-1]) // if number 1 > number 2
{
// save nums1's element into nums1's last "empty" spot.
nums1[m+n-1] = nums1[m-1];
m--;
}
else // if number 1 <= number 2
{
// save nums2's element into nums1's last "empty" spot
nums1[m+n-1] = nums2[n-1];
n--;
}
} // check if nums2's elements are not finished.
while(n > 0)
{
// save nums2's rest elements into nums1
nums1[m+n-1] = nums2[n-1];
n--;
}
}
}

参考资料:

http://www.cnblogs.com/springfor/p/3872640.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 88. Merge Sorted Array(合并有序数组)的更多相关文章

  1. [LeetCode] 88. Merge Sorted Array 合并有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  2. [leetcode]88. Merge Sorted Array归并有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  3. Leetcode#88. Merge Sorted Array(合并两个有序数组)

    题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

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

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  5. Leetcode 88 Merge Sorted Array STL

    合并有序数组 时间复杂度O(m+n) 该算法来自各种算法与数据结构书,写得已经烂得不能再烂了,这个应该是最短的代码了吧,不知如何归类 class Solution { public: void mer ...

  6. 【LeetCode】Merge Sorted Array(合并两个有序数组)

    这道题是LeetCode里的第88道题. 题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nu ...

  7. LeetCode 088 Merge Sorted Array 合并两个有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You ...

  8. leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...

  9. LeetCode 88 Merge Sorted Array

    Problem: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...

随机推荐

  1. Eclipse rap 富客户端开发总结(11) : rcp/rap与spring ibatis集成

    1. rcp/rap 与 spring 集成 Activator 是rcp/rap 启动时需要加载的类, 只需要加载一遍,所以与spring 集成的时候一般是在这个类里面加载spring 的Appli ...

  2. 浅谈IT技术女转战微电商初体验

    今天闲来无事,突然想翻看下之前写的技术博客,很是意外,居然那么多阅读量,于是想想做微商也有一段时间了,决定写写初入微商的初体验. 先自我介绍一下,本人是一名理工女,做IT行业的,这个行业也许有人了解, ...

  3. BlockingQueue<> 队列的作用

    BlockingQueue<> 队列的作用 BlockingQueue 实现主要用于生产者-使用者队列 BlockingQueue 实现主要用于生产者-使用者队列,BlockingQueu ...

  4. 浏览器缓存机制<转>

    转这篇文章是感觉可以在图片加载的时候,也使用这样的缓存策略   作者:吴秦出处:http://www.cnblogs.com/skynet/本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或 ...

  5. ”TCP连接“究竟是什么意思?

    我们经常听到"建立TCP连接","服务器的连接数量有限"等,但仔细一想,连接究竟是个什么东西,是和电话一样两端连起一根线?似乎有点抽象不是么? 1. 久违的分组 ...

  6. ArrayList,LinkedListd等容器使用时注意点:

    1.对这两个List(包括其他的类似容器),如果向里面加入一个元素(引用数据类型),那么这个List里面保存的是这个对象的引用: 如果想要避免这种现象可以这样:在加入新的元素时不直接压,将已有的对象复 ...

  7. 在Pycharm中使用jupyter笔记本

    在Pycharm中使用jupyter笔记本 我在Pycharm中使用jupyter笔记本,发现新的Jupyter更新中,增加了令牌. 随着创建的虚拟环境启动的所有设置,并将URL设置为127.0.0. ...

  8. 使用java实现面向对象-File I/O

    java.io.File类用于表示文件(目录) File类只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问 RandomAccessFile java提供的对文件内容的访问,既可以 ...

  9. PIC32MZ 通过USB在线升级 -- USB HID bootloader

    了解 bootloader 的实现, 请加QQ: 1273623966(验证填bootloader); 欢迎咨询或定制bootloader; 我的博客主页 www.cnblogs.com/geekyg ...

  10. linux上搭建ftp

    linux上搭建ftp 重要 解决如何搭建ftp         解决用户指定访问其根目录         解决访问ftp超时连接         解决ftp主动连接.被动连接的问题 1.安装ftp ...