当前位置:网站首页>【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 边栏推荐
- After reading the average code written by Microsoft God, I realized that I was still too young
- Global and Chinese market of network connected IC card smart water meters 2022-2028: Research Report on technology, participants, trends, market size and share
- Single step debugging of master data reading of SAP commerce cloud products
- Package What is the function of JSON file? What do the inside ^ angle brackets and ~ tilde mean?
- Query for Boolean field as "not true" (e.g. either false or non-existent)
- Introduction to the gtid mode of MySQL master-slave replication
- 大专学历,33岁宝妈又怎样?我照样销售转测试,月入13k+
- BGP comprehensive experiment
- Remote control service
- LeetCode周赛 + AcWing周赛(T4/T3)分析对比
猜你喜欢

Analysis and comparison of leetcode weekly race + acwing weekly race (t4/t3)

【海浪建模3】三维随机真实海浪建模以及海浪发电机建模matlab仿真

Poap: the adoption entrance of NFT?

【大型电商项目开发】性能压测-性能监控-堆内存与垃圾回收-39

Pycharm professional download and installation tutorial

Huawei employs millions of data governance experts! The 100 billion market behind it deserves attention

揭露测试外包公司,关于外包,你或许听到过这样的声音

"Upside down salary", "equal replacement of graduates" these phenomena show that the testing industry has

Behind the cluster listing, to what extent is the Chinese restaurant chain "rolled"?

There is a new Post-00 exam king in the testing department. I really can't do it in my old age. I have
随机推荐
【海浪建模2】三维海浪建模以及海浪发电机建模matlab仿真
Database postragesq PAM authentication
Playwright recording
I was beaten by the interviewer because I didn't understand the sorting
Global and Chinese market of network connected IC card smart water meters 2022-2028: Research Report on technology, participants, trends, market size and share
There is a new Post-00 exam king in the testing department. I really can't do it in my old age. I have
微信小程序:全新独立后台月老办事处一元交友盲盒
Introduction to redis (1)
Pandora IOT development board learning (RT thread) - Experiment 4 buzzer + motor experiment [key external interrupt] (learning notes)
SAP UI5 应用的主-从-从(Master-Detail-Detail)布局模式的实现步骤
如果消费互联网比喻成「湖泊」的话,产业互联网则是广阔的「海洋」
If the consumer Internet is compared to a "Lake", the industrial Internet is a vast "ocean"
Digital DP template
La jeunesse sans rancune de Xi Murong
Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
【FPGA教程案例9】基于vivado核的时钟管理器设计与实现
【大型电商项目开发】性能压测-性能监控-堆内存与垃圾回收-39
Database postragesql client authentication
Daily question brushing record (13)
Check if this is null - checking if this is null
