当前位置:网站首页>合并有序数列
合并有序数列
2022-07-02 06:36:00 【迷途~知返】
合并有序数列
给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。
请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-sorted-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
知识点;
数组:
列表的高级特性,切片
个人思路:
- 删除nums1数组中的0元素
- 添加nums2中的元素到nums1中
- nums1调用sort方法对新数组进行排序
在删除0元素的过程中,nums1中的最后一个0元素始终没有删除掉
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: None Do not return anything, modify nums1 in-place instead. """
# nums1[m:] = nums2
# nums1.sort()
# print nums1
for i in range(n):
nums1.append(nums2[i])
print nums1
l = len(nums1)
print l
for i in range(l):
print (i, nums1[i])
if nums1[i] == 0:
nums1.pop(i)
nums1.sort()
解法一:
使用函数的高级特性:列表切片
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: None Do not return anything, modify nums1 in-place instead. """
nums1[m:]=nums2
nums1.sort()
直接合并:nums1和nums2
m表示nums1中的非0元素的个数
n表示nums2中的非0元素的个数
将nums1中m位后的0元素直接用nums2中的元素替换
时间复杂度:根据快速排序可计算出时间复杂度
边栏推荐
- 2837xd code generation module learning (4) -- idle_ task、Simulink Coder
- Illusion -- Animation blueprint, state machine production, character walking, running and jumping action
- 阿里云SLS日志服务
- UE illusory engine programmed plant generator setup -- how to quickly generate large forests
- 虚幻材质编辑器基础——如何连接一个最基本的材质
- Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
- [IDL] Research
- How does {} prevent SQL injection? What is its underlying principle?
- 阿里云Prometheus监控服务
- How to achieve the top progress bar effect in background management projects
猜你喜欢

Vscode set JSON file to format automatically after saving

AutoCAD - layer Linetype

阿里云SLS日志服务

What wires are suitable for wiring on bread board?
![[ue5] blueprint making simple mine tutorial](/img/87/d0bec747a6b6276d63a315f88745ec.png)
[ue5] blueprint making simple mine tutorial

Message mechanism -- getting to know messages and message queues for the first time

Alibaba cloud Prometheus monitoring service

It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
![[Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology](/img/a9/ffd5d8000fc811f958622901bf408d.png)
[Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology

2837xd code generation - stateflow (2)
随机推荐
Brief analysis of edgedb architecture
【虚幻】过场动画笔记
SAP Spartacus express checkout design
【UE5】AI随机漫游蓝图两种实现方法(角色蓝图、行为树)
【避坑指南】Unity3D项目接入腾讯Bugly工具时遇到的坑
虚幻——动画蓝图、状态机制作人物走跑跳动作
Career planning and development
pytest--之测试报告allure配置
What is the relationship between realizing page watermarking and mutationobserver?
滲透測試的介紹和防範
【虚幻4】从U3D到UE4的转型之路
go语言入门
Database -- acid of transaction -- introduction / explanation
Blender多镜头(多机位)切换
Pycaret | a few lines of code to solve machine learning modeling
C language strawberry
Configuration programmée du générateur de plantes du moteur illusoire UE - - Comment générer rapidement une grande forêt
阿里云SLS日志服务
Notes de base sur les plans illusoires d'IA (triés en 10 000 mots)
Junit5 支持suite的方法