当前位置:网站首页>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
边栏推荐
- Bookmark collection management software suspension reading and data migration between knowledge base and browser bookmarks
- Junit4 runs MVN test test suite upgrade scheme
- 【教程】如何让VisualStudio的HelpViewer帮助文档独立运行
- Spatial interpretation | comprehensive analysis of spatial structure of primary liver cancer
- Sil/pil test of matlab code generation
- Summary of demand R & D process nodes and key outputs
- 【避坑指南】Unity3D项目接入腾讯Bugly工具时遇到的坑
- Mysql索引
- 2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?
- 2837xd code generation module learning (1) -- GPIO module
猜你喜欢

Unreal material editor foundation - how to connect a basic material

Alibaba cloud ack introduction

pytest--之测试报告allure配置

Blender multi lens (multi stand) switching

Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer

ICLR 2022: how does AI recognize "things I haven't seen"?

Skywalking理论与实践

ue虛幻引擎程序化植物生成器設置——如何快速生成大片森林

Configuration programmée du générateur de plantes du moteur illusoire UE - - Comment générer rapidement une grande forêt

2837xd code generation module learning (3) -- IIC, ECAN, SCI, watchdog, ECAP modules
随机推荐
Project practice, redis cluster technology learning (16)
07数据导入Sqoop
2837xd代码生成模块学习(1)——GPIO模块
ICLR 2022: how does AI recognize "things I haven't seen"?
XA Transaction SQL Statements
go语言入门
Project practice, redis cluster technology learning (10)
【虚幻4】从U3D到UE4的转型之路
Blender volume fog
MySQL index
2837xd code generation module learning (2) -- ADC, epwm module, timer0
Project practice, redis cluster technology learning (VII)
【UE5】蓝图制作简单地雷教程
【MySQL】连接MySQL时出现异常:Connection must be valid and open
Project practice, redis cluster technology learning (12)
Blender多鏡頭(多機比特)切換
Blender海洋制作
【避坑指南】Unity3D项目接入腾讯Bugly工具时遇到的坑
[IDL] Research
Career planning and development