当前位置:网站首页>【LeetCode】88. Merge two ordered arrays
【LeetCode】88. Merge two ordered arrays
2022-07-05 01:20:00 【onlywishes】
Ask for 2 The element in is added to 1 Medium and finally orderly
Their thinking :
One 、 Put... Directly 2 The element in is added to 1 The elements in are followed by sorting
nums1[m:]=nums2 # take 1 After the element in 0 Replace with 2 Medium element
nums1.sort() # Sort the whole
Two 、 Set two pointers , With the help of an empty list , Yes 1 and 2 Compare sizes starting with the first element , Put the small ones in the list , End of traversal 1,2 After array , Assign the ordered list to 1
ls = []
p1 ,p2 =0,0
while p1 <m or p2<n : # Merge the element comparison sizes into one
if p1 ==m: #nums1 period , Just put 2 Join in
ls.append(nums2[p2])
p2+=1
elif p2==n: #nums2 period , Just put 1 Join in
ls.append(nums1[p1])
p1+=1
elif nums1[p1] < nums2[p2]:
ls.append(nums1[p1])
p1+=1
else:
ls.append(nums2[p2])
p2+=1
nums1[:]=ls # Yes nums1 Slice from scratch , hold ls Give it
3、 ... and 、 Set two pointers , because 1 in m The back is empty , Can be assigned at will , You can go directly to 1 Add and sort elements on
take 1 and 2 Compare with the maximum value in , Is the last element comparison , Big on 1 Last position in , Every time , to update 1 Last position in , until 1 and 2 All traverse from back to front , That's the order
p1 = m-1 #1 Middle and end elements ,
p2 = n-1 #2 Middle and end elements
p = m+n-1 # Definition 1 Position of the last element in
while p1>=0 or p2>=0:
if p1<0:
nums1[p] = nums2[p2] #1 There is no , Join in 2
p2-=1
elif p2<0:
nums1[p] = nums1[p1] #2 Nothing , Join in 1
p1-=1
elif nums1[p1]<nums2[p2]:
nums1[p] = nums2[p2]
p2-=1
else:
nums1[p] = nums1[p1]
p1-=1
p-=1 # Update end position
边栏推荐
- Hand drawn video website
- Nebula importer data import practice
- "Upside down salary", "equal replacement of graduates" these phenomena show that the testing industry has
- Digital DP template
- 抓包整理外篇——————状态栏[ 四]
- Discrete mathematics: Main Normal Form (main disjunctive normal form, main conjunctive normal form)
- College degree, what about 33 year old Baoma? I still sell and test, and my monthly income is 13K+
- 微信小程序:全新独立后台月老办事处一元交友盲盒
- 微信小程序:全网独家小程序版本独立微信社群人脉
- Les phénomènes de « salaire inversé » et de « remplacement des diplômés » indiquent que l'industrie des tests a...
猜你喜欢
User login function: simple but difficult
小程序直播 + 电商,想做新零售电商就用它吧!
Yyds dry goods inventory [Gan Di's one week summary: the most complete and detailed in the whole network]; detailed explanation of MySQL index data structure and index optimization; remember collectio
LeetCode周赛 + AcWing周赛(T4/T3)分析对比
SAP ui5 application development tutorial 107 - trial version of SAP ui5 overflow toolbar container control introduction
POAP:NFT的采用入口?
Blue Bridge Cup Square filling (DFS backtracking)
Remote control service
The performance of major mainstream programming languages is PK, and the results are unexpected
BGP comprehensive experiment
随机推荐
[Yocto RM]11 - Features
Discrete mathematics: Main Normal Form (main disjunctive normal form, main conjunctive normal form)
Database postragesql client authentication
微信小程序:星宿UI V1.5 wordpress系统资讯资源博客下载小程序微信QQ双端源码支持wordpress二级分类 加载动画优化
Game 280 of leetcode week
The server time zone value ‘� й ��� ʱ 'is unrecognized or representatives more than one time zone【
Are you still writing the TS type code
Expose testing outsourcing companies. You may have heard such a voice about outsourcing
Hand drawn video website
微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
Jcenter () cannot find Alibaba cloud proxy address
Mongodb series learning notes tutorial summary
实战模拟│JWT 登录认证
Poap: the adoption entrance of NFT?
node工程中package.json文件作用是什么?里面的^尖括号和~波浪号是什么意思?
[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #
When the industrial Internet era is truly developed and improved, it will witness the birth of giants in every scene
【微处理器】基于FPGA的微处理器VHDL开发
[wave modeling 1] theoretical analysis and MATLAB simulation of wave modeling
Classification of performance tests (learning summary)