当前位置:网站首页>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);
}
}
边栏推荐
- Extjs图书管理系统源码 智能化图书管理系统源码
- QCOM LCD调试
- 【牛客网刷题系列 之 Verilog快速入门】~ 四选一多路器
- [csp-j2020] excellent splitting
- Find an SQL that can judge the data in the table and only fill in the SQL that is not overwritten
- metaRTC5.0 API编程指南(一)
- 二级造价工程师证书含金量到底有多高?看这些就知道了
- 浅析搭建视频监控汇聚平台的必要性及场景应用
- [applet] solution document using font awesome Font Icon (picture and text)
- PHP code wechat, official account and enterprise wechat send emoticons [u+1f449]
猜你喜欢
机器人学DH参数及利用matlab符号运算推导
玩转双指针
[applet] solution document using font awesome Font Icon (picture and text)
Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems
Where does the storm go? Whose pot is the weather forecast wrong?
Don't roll! How to reproduce a paper with high quality?
学习太极创客 — MQTT 第二章(五)心跳机制
Distributed transaction - Final consistency scheme based on message compensation (local message table, message queue)
灵活的IP网络测试工具——— X-Launch
Why is the frame rate calculated by opencv wrong?
随机推荐
Mask's miserable and inspirational childhood, who is introverted by campus violence
为什么大厂不让使用undefined
100+ data science interview questions and answers Summary - machine learning and deep learning
活性染料研究:Lumiprobe AF594 NHS 酯,5-异构体
BioVendor sRAGE Elisa试剂盒化学性质和技术研究
学习太极创客 — MQTT 第二章(五)心跳机制
Analysis of distributed transaction TCC
2022电力电缆判断题模拟考试平台操作
native关键字的作用
2022高处安装、维护、拆除考试题及答案
Severe tire damage: the first rock band in the world to broadcast live on the Internet
高通平台 Camera 之 MCLK 配置
Audio and video technology development weekly
恭喜我自己,公众号粉丝破万
Taco: a data enhancement technique for character recognition
多线程实现 重写run(),怎么注入使用mapper文件操作数据库
Detailed reading of the thesis: implementing volume models for handowriting text recognition
Sword finger offer 47 Maximum gift value (DP)
Code understanding: implementing volume models for hangwriten text recognition
基于微信小程序的婚纱影楼门户小程序