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 ...
随机推荐
- vue项目单页
<template> <div> <div v-if="type === 'A'">A</div> <div v-else-i ...
- 解决ERROR 1130: Host 'x.x.x.x' is not allowed to connect to this MariaDB server 方法
问题描述 在使用SQLyog操作Linux上的MariaDB时候,会出现如下错误: 解决方法 改表法 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电 ...
- [CERC2014] Outer space invaders
题目链接 题意 你受到一群外星人的攻击,第 $i$ 个外星人会在 $ai$ 时间出现,与你的距离为 $di$,且必须在 $bi$ 时间前消灭.你有一个区域冲击波器,每次攻击可以设定一个功率 $R$,这 ...
- #! /usr/bin node 和#! /usr/bin/env node两者的区别
是Unix和Linux脚本语言的第一行,目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它 !/usr/bin/node是告诉操作系统执行这个脚本的时候,调用/usr/bin下的node ...
- vue引用fastClick后,ios输入框聚焦不灵敏问题
fastClick.prototype.focus = function (targetElement) { targetElement.focus() }
- IIS虚拟目录
https://blog.csdn.net/mianyao1004/article/details/94036169
- 关于MySQL的tinyint(3)问题
mysql 中int(1)和tinyint(1)中的1只是指定显示长度,并不表示存储长度.tinyint可以存储1字节, 即unsigned 0~255(signed -127~127).显示大小不受 ...
- python 中if和elif的区别
如果程序中判断事件很多,全部用if的话,会遍历整个程序,用elif 程序运行时,只要if或后续某一个elif之一满足逻辑值为True,则程序执行完对应输出语句后自动结束该轮if-elif(即不会再去冗 ...
- HTML学习(8)超链接
<a href="url">链接文本或图片</a> 可以使用id属性来访问标记的地方,例: <a id="tip">被访问的 ...
- 【vue】axios + cookie + 跳转登录方法
axios 部分: import axios from 'axios' import cookie from './cookie.js' // import constVal from './cons ...