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

Note:

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

Example:

Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] 这个题目思路就是用two pointers, 我想的是用三个指针, 一个m指向最后nums1的最后一个, 一个n指向nums2的最后一个, 一个l指向m+n-1的最后一个.然后分别比较m 与n,依次将最大的值放在l上,
如果n == 0,也就是说第二个数组nums2已经全移上来了,那么就结束while loop; 而如果是m== 0 的话, 我们就继续移动nums2, 直到nums2被移动完为止.
 
class Solution:
def mergeTwoSortedArray(self, nums1, m, nums2, n):
l = m + n -1
while n >0:
if m == 0 or nums2[n-1] > nums1[m-1]:
nums1[l] = nums2[n-1]
n -= 1
else:
nums1[l] = nums1[m-1]
m -= 1
l -= 1

[LeetCode] 88. Merge Sorted Array_Easy tag: Two Pointers的更多相关文章

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

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

  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 合并有序数组

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

  4. LeetCode 88. Merge Sorted Array(合并有序数组)

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

  5. LeetCode 88 Merge Sorted Array

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

  6. Leetcode 88. Merge Sorted Array(easy)

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

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

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

  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 STL

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

随机推荐

  1. Javascript常见性能优化

    俗话说,时间就是生命,时间就是金钱,时间就是一切,人人都不想把时间白白浪费,一个网站,最重要的就是体验,而网站好不好最直观的感受就是这个网站打开速度快不快,卡不卡. 当打开一个购物网站卡出翔,慢的要死 ...

  2. Django配合MySQL学习Django模型外键的建立和使用

    Django 模型建立外键 在模型中建立外键是很简单的,基本操作如下 class Table(models.Model) column_name = models.ForeignKey(other-T ...

  3. 安装Windows Server 2012 R2提示"unable to create a new system partition or locate an existing system partition"解决方法

    重新安装Windows Server 2012 R2,把原来SSD分区全部格式化重建,用U盘启动安装时提示如下: "Setup was unable to create a new syst ...

  4. State Server实现多机器多站点 Session 共享 全手记

    网络环境有2台windows 2008 (192.168.1.71,192.168.1.72) 需要部署成 WebFarm,提高容错性. 网站部署在2台机器上的2个站点,如何才能做到Session的共 ...

  5. 【CF896D】Nephren Runs a Cinema 卡特兰数+组合数+CRT

    [CF896D]Nephren Runs a Cinema 题意:一个序列中有n格数,每个数可能是0,1,-1,如果一个序列的所有前缀和都>=0且总和$\in [L,R]$,那么我们称这个序列是 ...

  6. [工具] 各种主流 SQLServer 迁移到 MySQL 工具对比

    我之所以会写这篇对比文章,是因为公司新产品研发真实经历过这个痛苦过程(传统基于SQL Server开发的C/S产品转为MySQL云产品). 首次需要数据转换是测试环节,当时为了快速验证新研发云产品性能 ...

  7. 安装XP时BIOS的设置(ahci ide)

    和以前使用Windows XP一样,很多用户都在设法提高Windows 7的系统运行速速,比较常见的方法大多是对系统服务进行优化,去掉一些可有可无的系统服务,还有就是优化资源管理器菜单等.除此之外,还 ...

  8. 导入音乐到iPhone

    如果出现iCloud音乐资料库已打开 则需要在手机端设置:打开设置,找到音乐一栏,关闭icloud音乐资料库 http://baijiahao.baidu.com/s?id=1572411750316 ...

  9. pip freeze

    总结: 1.输出安装的包信息,并在另一个环境快速安装 Generate output suitable for a requirements file. $ pip freeze docutils== ...

  10. a new way of thinking about a problem

    PHP Advanced and Object-Oriented Programming Larry Ullman   The first thing that you must understand ...