当前位置:网站首页>Leetcode 88: merge two ordered arrays
Leetcode 88: merge two ordered arrays
2022-06-28 04:59:00 【Swarford】
subject :
Note that the array is already in ascending order by default .
Method 1 :
Use double pointer p1 p2 Traversing two arrays ;
Process and Merge two ordered linked lists similar
When p1 p2 I haven't traversed to the end yet , Then judge from front to back p1 p2 The element pointed to by the pointer , Put the small one in r Array ;
When any array ends , And then jump out of the loop , Continue adding the array that has not reached the end to r The array can be ;
Last copy to nums1.
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int[] r=new int[nums1.length];
int p1=0;
int p2=0;
int curr=0;
while(p1<m && p2<n){
if(nums1[p1]<nums2[p2]){
r[curr++]=nums1[p1++];
}else{
r[curr++]=nums2[p2++];
}
}
// End of any array
while(p1<m){
r[curr++]=nums1[p1++];
}
while(p2<n){
r[curr++]=nums2[p2++];
}
System.arraycopy(r,0,nums1,0,n+m);
}
}
Method 2 :Arrays.sort
First the nums2 Add directly to nums1, And then use Java Bring their own API Sort
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
for(int i=0;i<n;i++){
nums1[m+i]=nums2[i];
}
Arrays.sort(nums1);
}
}
边栏推荐
- [csp-j2020] excellent splitting
- Generate QR code in wechat applet
- 活性染料研究:Lumiprobe AF594 NHS 酯,5-异构体
- 基于微信小程序的婚纱影楼门户小程序
- Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
- Role of native keyword
- 2022 low voltage electrician examination questions and answers
- lotus v1.16.0 calibnet
- 【牛客网刷题系列 之 Verilog快速入门】~ 四选一多路器
- RxSwift --(1)创建一个项目
猜你喜欢

改性三磷酸盐研究:Lumiprobe氨基-11-ddUTP

基于订单流工具,我们能看到什么?

CUPTI error: CUPTI could not be loaded or symbol could not be found.

PCR/qPCR研究:Lumiprobe丨dsGreen 用于实时 PCR

wordpress zibll子比主题6.4.1开心版 免授权

如何从零设计一款牛逼的高并发架构(建议收藏)

活性染料研究:Lumiprobe AF594 NHS 酯,5-异构体

2022年安全员-A证考试题库及模拟考试

CUPTI error: CUPTI could not be loaded or symbol could not be found.

Operation of simulated examination platform of G3 boiler water treatment recurrent training question bank in 2022
随机推荐
2022 high altitude installation, maintenance and removal examination questions and answers
Audio and video technology development weekly
学习太极创客 — MQTT 第二章(四)ESP8266 保留消息应用
lotus v1.16.0 calibnet
判断对象中是否存在某一个属性
Congratulations to myself, official account has more than ten thousand fans
Understanding the source of innovation II
Multi thread implementation rewrites run (), how to inject and use mapper file to operate database
Oracledata installation problems
DH parameters of robotics and derivation using MATLAB symbolic operation
程序员坐牢了,会被安排去写代码吗?
Standard particle swarm optimization C language program
Feign implements path escape through custom annotations
Function and working principle of controller
2022年G3锅炉水处理复训题库模拟考试平台操作
mysql修改密码报错需要怎么做
几百行代码实现一个脚本解释器
控制器的功能和工作原理
Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems
?位置怎么写才能输出true