当前位置:网站首页>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
边栏推荐
- High level application of SQL statements in MySQL database (II)
- How to judge the quality of primary market projects when the market is depressed?
- C language: making barrels
- Large neural networks may be beginning to realize: the chief scientist of openai leads to controversy, and everyone quarrels
- Project practice, redis cluster technology learning (10)
- 【Visual Studio】每次打开一个Unity3D的脚本,都会自动重新打开一个新的VS2017
- Nonlinear optimization: establishment of slam model
- AutoCAD - layer Linetype
- 虚幻AI蓝图基础笔记(万字整理)
- Blender摄像机环绕运动、动画渲染、视频合成
猜你喜欢

【JetBrain Rider】构建项目出现异常:未找到导入的项目“D:\VisualStudio2017\IDE\MSBuild\15.0\Bin\Roslyn\Microsoft.CSh

Application of rxjs operator withlatestfrom in Spartacus UI of SAP e-commerce cloud

High level application of SQL statements in MySQL database (II)

Blender ocean production

Illusion -- Animation blueprint, state machine production, character walking, running and jumping action

Unreal material editor foundation - how to connect a basic material

Mysql索引

Network real-time video streaming based on OpenCV

【UE5】蓝图制作简单地雷教程

阿里云ack介绍
随机推荐
The latest progress and development trend of 2022 intelligent voice technology
[ue5] animation redirection: how to import magic tower characters into the game
Configuration programmée du générateur de plantes du moteur illusoire UE - - Comment générer rapidement une grande forêt
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
Project practice, redis cluster technology learning (11)
Blender摄像机环绕运动、动画渲染、视频合成
Brief analysis of edgedb architecture
【UE5】蓝图制作简单地雷教程
UE5——AI追逐(蓝图、行为树)
Summary of demand R & D process nodes and key outputs
Message mechanism -- getting to know messages and message queues for the first time
ERROR 1118 (42000): Row size too large (> 8126)
【虚幻】武器插槽:拾取武器
Pycaret | a few lines of code to solve machine learning modeling
AutoCAD - layer Linetype
Aiphacode is not a substitute for programmers, but a tool for developers
Vscode set JSON file to format automatically after saving
【虚幻4】从U3D到UE4的转型之路
How to achieve the top progress bar effect in background management projects
Project practice, redis cluster technology learning (6)