当前位置:网站首页>合并有序数列
合并有序数列
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中的元素替换
时间复杂度:根据快速排序可计算出时间复杂度
边栏推荐
- Remember the use of add method once
- Translation d30 (with AC code POJ 28:sum number)
- Mixed development of uni app -- Taking wechat applet as an example
- Blender stone carving
- Aiphacode is not a substitute for programmers, but a tool for developers
- Leetcode -- the nearest common ancestor of 236 binary tree
- Following nym, the new project Galaxy token announced by coinlist is gal
- Postman--使用
- Attack and defense world web advanced area unserialize3
- Career planning and development
猜你喜欢
Blender camera surround motion, animation rendering, video synthesis
UE4 night lighting notes
VLAN experiment
Blender体积雾
Junit4运行mvn test 测试套件升级方案
Basic notes of illusory AI blueprint (10000 words)
Blender multi lens (multi stand) switching
[ue5] blueprint making simple mine tutorial
Notes de base sur les plans illusoires d'IA (triés en 10 000 mots)
Summary of demand R & D process nodes and key outputs
随机推荐
【虚幻】自动门蓝图笔记
2837xd code generation module learning (4) -- idle_ task、Simulink Coder
ESLint 报错
Blender camera surround motion, animation rendering, video synthesis
【MySQL】连接MySQL时出现异常:Connection must be valid and open
Eslint reports an error
MySQL -- time zone / connector / driver type
Ctrip starts mixed office. How can small and medium-sized enterprises achieve mixed office?
Tee command usage example
Project practice, redis cluster technology learning (VII)
2837xd code generation module learning (1) -- GPIO module
Spatial interpretation | comprehensive analysis of spatial structure of primary liver cancer
MySQL transaction
Centos7 one click compilation and installation of PHP script
【UE5】蓝图制作简单地雷教程
Blender volume fog
UE4 night lighting notes
Following nym, the new project Galaxy token announced by coinlist is gal
ue虛幻引擎程序化植物生成器設置——如何快速生成大片森林
【虚幻】过场动画笔记