[LeetCode]题解(python):088 Merge Sorted Array
题目来源
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的更多相关文章
- LeetCode练题——88. Merge Sorted Array
1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into ...
- LeetCode 088 Merge Sorted Array 合并两个有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You ...
- Java for LeetCode 088 Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 解题思路一: ...
- [LeetCode 题解] Search in Rotated Sorted Array
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目描述 Suppose an array ...
- LeetCode(40)-Merge Sorted Array
听到初爱有感 开头啰嗦两句,刚在做算法题目的时候,听到了杨宗纬的<初爱>,突然有了一种本科时候的感觉,想想自己现在研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐-.. 题目: Gi ...
- LeetCode(88)Merge Sorted Array
题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note ...
- 088 Merge Sorted Array 合并两个有序数组
给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1中,使得 num1 成为一个有序数组.注意:你可以假设 nums1有足够的空间(空间大小大于或等于m + n)来保存 ...
- 【LeetCode 88_数组】Merge Sorted Array
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { ; ; ; &&a ...
- [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 ...
随机推荐
- Girls and Boys
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- SecureCrt脚本(一)顶级对象之Crt
Crt自动化 测试 SecureCrt脚本 JS脚本 1.引言 2.关于脚本表头 3.顶级对象'crt'的子属性和方法 3.1.属性 3.1.1.Dialog 3.1.2.Screen 3.1.3 ...
- mysql中datetime比较大小问题 (转的)
方法一: 你也可以:select * from t1 where unix_timestamp(time1) > unix_timestamp('2011-03-03 17:39:05') an ...
- jsoncpp封装和解析字符串、数字、布尔值和数组
使用jsoncpp进行字符串.数字.布尔值和数组的封装与解析. 1)下载jsoncpp的代码库 百度网盘地址 :http://pan.baidu.com/s/1ntqQhIT 2)解压缩文件 json ...
- CustomValidator验证的使用方法
<asp:TextBox ID="txtNum" runat="server" Width="400px" ></asp: ...
- 常用的一些webshell木马官方后门
80SEC剑心修改过世界杀毒版 http://wwwxx.cn/1.asp?web2a2dmin=//?web2a2dmin=//1.asp 绕过 不灭之魂的后门 1.在错误页面右键可以查看密码 2 ...
- GDB打印STL容器内容
GDB调试不能打印stl容器内容,下载此文件,将之保存为~/.gdbinit就可以使用打印命令了. 打印list用plist命令,打印vector用pvector,依此类推. (gdb) pvecto ...
- Excel 中单元格和范围的引用(即访问的表示方法)
计算机中,无非是数据和数据的处理这两件事.Excel的工作表能存储大量数据,除了这些原始数据,我们还要用函数来处理这些数据,比如求和求积,求平均值,排序等等,并把处理结果也存在单元格里.在Excel中 ...
- RESTful和JAX-RS
一.简介 Java Web有很多成熟的框架,主要可以分为两类Web Application和Web Services.用于Web Application的框架包括官方的Servlet/JSP, JST ...
- PHP 错误与异常 笔记与总结(2)错误(Fatal)
(接上) d.Fatal error 致命级别的错误 —— 程序终止执行 [例7]调用一个未定义的方法 <?php echo md6('dee'); echo 'continue'; 输出: ( ...