LeetCode练题——88. Merge Sorted Array
1、题目
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]
2、我的解答
# -*- coding: utf-8 -*-
# @Time : 2020/2/16 19:18
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 88. Merge Sorted Array.py
from typing import List class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
p = m + n - 1
p1 = m - 1
p2 = n - 1
while p1 >= 0 and p2 >= 0:
if nums2[p2] > nums1[p1]:
nums1[p] = nums2[p2]
p -= 1
p2 -= 1
else:
nums1[p] = nums1[p1]
p -= 1
p1 -= 1
while p2 >= 0:
nums1[p] = nums2[p2]
p -= 1
p2 -= 1 Solution().merge([1, 2, 5, 0, 0, 0], 3, [2, 5, 6], 3)
LeetCode练题——88. Merge Sorted Array的更多相关文章
- 【leetcode❤python】 88. Merge Sorted Array
#-*- coding: UTF-8 -*-class Solution(object): def merge(self, nums1, m, nums2, n): "& ...
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- [LeetCode] 88. Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- 【LeetCode】88. Merge Sorted Array (2 solutions)
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- 【一天一道LeetCode】#88. Merge Sorted Array
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- LeetCode Array Easy 88. Merge Sorted Array
Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar ...
- 【LeetCode】88. Merge Sorted Array 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 新建数组 日期 题目地址:https://leetc ...
- LeetCode 88. Merge Sorted Array(合并有序数组)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
随机推荐
- Linux下run文件的直接运行
比如realplay.run 安装方法如下 chmod +x realplay.run ./realplay.run 然后他就会执行安装了,在过程中可能会要求你输入yes或no 安装完后就可以用了 , ...
- 2.8 (显示、隐式、线程休眠) selenium 等待方式 ❀
http://blog.csdn.net/pf20050904/article/details/20052485 http://www.cnblogs.com/hellokitty1/p/629584 ...
- Linux中通配符
通配符是由shell处理的, 它只会出现在 命令的“参数”里.当shell在“参数”中遇到了通配符时,shell会将其当作路径或文件名去在磁盘上搜寻可能的匹配:若符合要求的匹配存在,则进行代换(路径扩 ...
- codeforces Make The Fence Great Again(dp)
题目链接:http://codeforces.com/contest/1221/problem/D 题目要求ai ! = ai-1,草纸上推理一下可以发现每一个栅栏可以升高的高度无非就是 +0,+1, ...
- (原)Vue 单文件组件安装 (创建vue-cli 项目)
更新于20200220 题外话:久违了我的博客园 正题: 1.准备工作,安装环境 1.安装node 官网下载安装即可 -- 配置环境变量 2.安装npm (基于node.js 包管理器) 3.安装c ...
- LabVIEW随笔2_毕业了
08年研究生毕业了,在原来公司的兼职也结束了,开始真正的工作生涯了. 在此,非常感恩我的导师綦院长,体谅我的困楚之处,在我的研究生学习期间给予我的宽容和照顾.不料,2014年,恩师五十有余,却突然离世 ...
- js Array 的所有方法
下面的这些方法会改变调用它们的对象自身的值: Array.prototype.copyWithin() 在数组内部,将一段元素序列拷贝到另一段元素序列上,覆盖原有的值. Array.prototyp ...
- 虚拟函数是否应该被声明仅为private/protected?
问题导入 我想对于大家来说,虚拟函数并不能算是个陌生的概念吧.至于怎么样使用它,大部分人都会告诉我:通过在子类中重写(override)基类中的虚拟函数,就可以达到OO中的一个重要特性——多态(pol ...
- opencv:自适应阈值
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- 【代码审计】seacms 前台Getshell分析
一.漏洞分析 漏洞触发点search.php 211-213行 跟进parseIf 函数 ./include/main.class.php 这里要注意 3118行的位置,可以看到未做任何处理的eval ...