题目来源


https://leetcode.com/problems/merge-sorted-array/

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 nums1and nums2 are m and n respectively.


题意分析


Input:

  :type nums1: List[int]
  :type m: int
  :type nums2: List[int]
  :type n: int

Output:

:rtype: void Do not return anything, modify nums1 in-place instead.

Conditions:合并两个sorted的list,注意是将nums2合并进nums1当中,结果也必须是sorted,不返回任何元素。


题目思路

注意到nums1是有足够的空间存储m+n个元素的,所以直接对nums1进行操作就好,选择从数值较大的位置开始选择元素。

备注:如果n小于0了,就不需要继续合并了。


AC代码(Python)


 class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
all = m + n - 1
m = m - 1
n = n - 1
while m >= 0 and n >= 0:
if nums1[m] > nums2[n]:
nums1[all] = nums1[m]
m = m - 1
all = all - 1
else:
nums1[all] = nums2[n]
n = n - 1
all = all - 1
while n >= 0:
nums1[all] = nums2[n]
all = all - 1
n = n - 1

[LeetCode]题解(python):088 Merge Sorted Array的更多相关文章

  1. LeetCode练题——88. Merge Sorted Array

    1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into  ...

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

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

  3. Java for LeetCode 088 Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 解题思路一: ...

  4. [LeetCode 题解] Search in Rotated Sorted Array

    前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目描述 Suppose an array ...

  5. LeetCode(40)-Merge Sorted Array

    听到初爱有感 开头啰嗦两句,刚在做算法题目的时候,听到了杨宗纬的<初爱>,突然有了一种本科时候的感觉,想想自己现在研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐-.. 题目: Gi ...

  6. LeetCode(88)Merge Sorted Array

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

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

    给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1中,使得 num1 成为一个有序数组.注意:你可以假设 nums1有足够的空间(空间大小大于或等于m + n)来保存 ...

  8. 【LeetCode 88_数组】Merge Sorted Array

    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { ; ; ; &&a ...

  9. [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 ...

随机推荐

  1. CodeForces Round 192 Div2

    This is the first time I took part in Codeforces Competition.The only felt is that my IQ was contemp ...

  2. codevs 1507酒厂选址

    #include<cstdio> #include<cstdlib> using namespace std; int n; int dis[10010],a[10010],x ...

  3. telerik 某些ajax拿数据方式下 load on demand 不起作用

    方法一:加入以下代码 <%= Html.Telerik().ScriptRegistrar().DefaultGroup(group => { group.Add("teleri ...

  4. css样式表:样式分类,选择器。样式属性,格式与布局

    样式表分类: 1.内联样式表, 和html联合显示,例:<p style="font-size:14px;">内联样式表</p> 2.内嵌样式表 作为一个独 ...

  5. python基础数据类型

    整形:(不可变类型) >>> a = 123>>> type(a)<class 'int'> 长整形:(在python3中已经废弃了) >> ...

  6. 是否采用Sybase形式的自动字符串转义(用 '' 表示 ')

    ;; 关于php.ini ;; ; 这个文件必须命名为'php.ini'并放置在httpd.conf中PHPINIDir指令指定的目录中. ; 最新版本的php.ini可以在下面两个位置查看: ; h ...

  7. OpenSSL使用方法

    生成CA (勾选Generate Self Signed Certificate)openssl req -nodes -x509 -sha256 -newkey rsa:4096 -keyout & ...

  8. 根据BIOS信息修改主机名

    Dell: Rename-Computer -NewName ("CNHZPD-" + (Get-WmiObject -class win32_Bios).SerialNumber ...

  9. #在FLAT模式下,需要设置flat子网,VM的IP从这个设置的子网中抓取,这时flat_injected需要设置为True,系统才能自动获得IP,如果flat

    #在FLAT模式下,需要设置flat子网,VM的IP从这个设置的子网中抓取,这时flat_injected需要设置为True,系统才能自动获得IP,如果flat子网和主机网络是同一网络,网络管理员要注 ...

  10. Grunt教程——初涉Grunt

    前端自动化,这样的一个名词听起来非常的有吸引力,向往力.当今时代,前端工程师需要维护的代码变得及为庞大和复杂,代码维护.打包.发布等流程也 变得极为繁琐,同时浪费的时间和精力也越来越多,当然人为的错误 ...