当前位置:网站首页>合并有序数列
合并有序数列
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中的元素替换
时间复杂度:根据快速排序可计算出时间复杂度
边栏推荐
- This article takes you to learn in detail what is fiber to home FTTH
- Message mechanism -- getting to know messages and message queues for the first time
- 【虚幻】自动门蓝图笔记
- Summary of demand R & D process nodes and key outputs
- Network real-time video streaming based on OpenCV
- go语言入门
- How does {} prevent SQL injection? What is its underlying principle?
- Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
- Blender ocean production
- Centos7 one click compilation and installation of PHP script
猜你喜欢

Notes de base sur les plans illusoires d'IA (triés en 10 000 mots)

Applet development summary

2837xd code generation - Supplement (3)
![[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

Bookmark collection management software suspension reading and data migration between knowledge base and browser bookmarks

MySQL transaction

2837xd代码生成模块学习(2)——ADC、ePWM模块、Timer0

Blender多镜头(多机位)切换

Blender模型导入ue、碰撞设置

虚幻材质编辑器基础——如何连接一个最基本的材质
随机推荐
Blender ocean production
Blender multi lens (multi stand) switching
[IDL] Research
Project practice, redis cluster technology learning (VII)
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
Mock Server基本使用方法
2837xd代码生成模块学习(2)——ADC、ePWM模块、Timer0
Leetcode -- the nearest common ancestor of 236 binary tree
This monitoring system makes workers tremble: turnover intention and fishing can be monitored. After the dispute, the product page has 404
【虚幻】自动门蓝图笔记
How to judge the quality of primary market projects when the market is depressed?
职业规划和发展
【Unity3D】嵌套使用Layout Group制作拥有动态子物体高度的Scroll View
【Unity3D】无法正确获取RectTransform的属性值导致计算出错
2837xd Code Generation - Supplement (1)
Project practice, redis cluster technology learning (VIII)
Ctrip starts mixed office. How can small and medium-sized enterprises achieve mixed office?
Bookmark collection management software suspension reading and data migration between knowledge base and browser bookmarks
UE illusory engine programmed plant generator setup -- how to quickly generate large forests
[illusory] automatic door blueprint notes