当前位置:网站首页>【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 边栏推荐
- Grabbing and sorting out external articles -- status bar [4]
- 实战模拟│JWT 登录认证
- 如果消费互联网比喻成「湖泊」的话,产业互联网则是广阔的「海洋」
- JS implementation determines whether the point is within the polygon range
- Redis(1)之Redis简介
- SAP UI5 应用开发教程之一百零七 - SAP UI5 OverflowToolbar 容器控件介绍的试读版
- Wechat applet: wechat applet source code download new community system optimized version support agent member system function super high income
- Global and Chinese markets of radiation linear accelerators 2022-2028: Research Report on technology, participants, trends, market size and share
- Wechat applet: independent background with distribution function, Yuelao office blind box for making friends
- Hedhat firewall
猜你喜欢
![[wave modeling 2] three dimensional wave modeling and wave generator modeling matlab simulation](/img/50/b6cecc95e46fe1e445eb00ca415669.png)
[wave modeling 2] three dimensional wave modeling and wave generator modeling matlab simulation

大专学历,33岁宝妈又怎样?我照样销售转测试,月入13k+

Yyds dry goods inventory kubernetes management business configuration methods? (08)

What happened to those who focused on automated testing?
![[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #](/img/1c/deaf20d46e172af4d5e11c28c254cf.jpg)
[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #

Daily question brushing record (13)

Postman automatically fills headers

【海浪建模1】海浪建模的理论分析和matlab仿真
![Grabbing and sorting out external articles -- status bar [4]](/img/88/8267ab92177788ac17ab665a90b781.png)
Grabbing and sorting out external articles -- status bar [4]

Wechat applet: the latest WordPress black gold wallpaper wechat applet two open repair version source code download support traffic main revenue
随机推荐
Intel sapphire rapids SP Zhiqiang es processor cache memory split exposure
SAP ui5 application development tutorial 106 - how to improve the readability of SAP ui5 application routing URL trial version
【海浪建模1】海浪建模的理论分析和matlab仿真
【微处理器】基于FPGA的微处理器VHDL开发
Database postragesq BSD authentication
Analysis and comparison of leetcode weekly race + acwing weekly race (t4/t3)
微信小程序:星宿UI V1.5 wordpress系统资讯资源博客下载小程序微信QQ双端源码支持wordpress二级分类 加载动画优化
Grabbing and sorting out external articles -- status bar [4]
What you learned in the eleventh week
【海浪建模2】三维海浪建模以及海浪发电机建模matlab仿真
User login function: simple but difficult
After reading the average code written by Microsoft God, I realized that I was still too young
Compare whether two lists are equal
POAP:NFT的采用入口?
College degree, what about 33 year old Baoma? I still sell and test, and my monthly income is 13K+
视频网站手绘
What if the programmer's SQL data script coding ability is weak and Bi can't do it?
Arbitrum:二维费用
Armv8-a programming guide MMU (3)
整理混乱的头文件,我用include what you use
