当前位置:网站首页>Merge ordered sequence
Merge ordered sequence
2022-07-02 10:21:00 【Lost ~ know to return】
Ordered sequence after merging
Merge ordered sequence
Here are two buttons Non decreasing order Array of arranged integers nums1 and nums2, There are two other integers m and n , respectively nums1 and nums2 The number of elements in .
Would you please Merge nums2 To nums1 in , Make the merged array press Non decreasing order array
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/merge-sorted-array
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Knowledge point ;
Array :
Advanced features of lists , section
Personal thinking :
- Delete nums1 Array 0 Elements
- add to nums2 Elements in to nums1 in
- nums1 call sort Method to sort the new array
In the delete 0 In the process of element ,nums1 Last of 0 The element has never been deleted
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()
Solution 1 :
Use the advanced features of functions : List slice
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()
Direct merger :nums1 and nums2
m Express nums1 Middle Africa 0 Number of elements
n Express nums2 Middle Africa 0 Number of elements
take nums1 in m Post position 0 Element direct use nums2 Element replacement in
Time complexity : The time complexity can be calculated according to the quick sorting
边栏推荐
- 测试--面试题总结
- The primary market project galaxy will conduct public offering on coinlist on February 17
- 2837xd代码生成模块学习(1)——GPIO模块
- [ue5] animation redirection: how to import magic tower characters into the game
- 判断数组中是否存在重复元素
- 虛幻AI藍圖基礎筆記(萬字整理)
- Deep understanding of redis cache avalanche / cache breakdown / cache penetration
- Vscode set JSON file to format automatically after saving
- ue虛幻引擎程序化植物生成器設置——如何快速生成大片森林
- About the college entrance examination
猜你喜欢
判断数组中是否存在重复元素
ICLR 2022: how does AI recognize "things I haven't seen"?
UE4夜间打光笔记
【虚幻4】UMG组件的简介与使用(更新中...)
2021-10-02
测试--面试题总结
Network real-time video streaming based on OpenCV
Application of rxjs operator withlatestfrom in Spartacus UI of SAP e-commerce cloud
Ue5 - AI pursuit (blueprint, behavior tree)
【虚幻】自动门蓝图笔记
随机推荐
【Unity3D】嵌套使用Layout Group制作拥有动态子物体高度的Scroll View
Blender stone carving
07数据导入Sqoop
Applet development summary
Ue5 - ai Pursuit (Blueprint, Behavior tree)
XA Transaction SQL Statements
Matlab生成dsp程序——官方例程学习(6)
Brief analysis of edgedb architecture
判断数组中是否存在重复元素
【虚幻4】从U3D到UE4的转型之路
[illusory] weapon slot: pick up weapons
【UE5】动画重定向:如何将幻塔人物导入进游戏玩耍
2837xd code generation module learning (2) -- ADC, epwm module, timer0
How to handle error logic gracefully
渗透测试的介绍和防范
UE5——AI追逐(藍圖、行為樹)
Blender volume fog
Mysql索引
UE5——AI追逐(蓝图、行为树)
2021-10-04