当前位置:网站首页>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);
}
}
边栏推荐
- 短视频本地生活版块成为热门,如何把握新的风口机遇?
- lotus v1.16.0 calibnet
- 乔布斯在斯坦福大学的演讲稿——Follow your heart
- Taco: a data enhancement technique for character recognition
- 2022年安全员-B证考试题库及答案
- RxSwift --(1)创建一个项目
- 2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
- Role of native keyword
- CPG 固体支持物研究:Lumiprobe通用 CPG II 型
- 100+ data science interview questions and answers Summary - machine learning and deep learning
猜你喜欢

Huawei's 9-year experience as a software testing director

Severe tire damage: the first rock band in the world to broadcast live on the Internet

Multi thread implementation rewrites run (), how to inject and use mapper file to operate database

Sword finger offer 53 - I. find the number I in the sorted array (improved bisection)

2022 safety officer-b certificate examination question bank and answers

CPG 固体支持物研究:Lumiprobe通用 CPG II 型

Generate QR code in wechat applet

2022 low voltage electrician examination questions and answers

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

Light collector, Yunnan Baiyao!
随机推荐
S32ds jump to defaultisr
使用class toplevel的messagebox时,窗口弹出问题。
Analysis of distributed transaction TCC
恭喜我自己,公众号粉丝破万
DH parameters of robotics and derivation using MATLAB symbolic operation
项目经理考完PMP就够了?不是的!
Multi thread implementation rewrites run (), how to inject and use mapper file to operate database
浅析搭建视频监控汇聚平台的必要性及场景应用
通过例子学习Rust
判断对象中是否存在某一个属性
Audio and video technology development weekly
Function and working principle of controller
cgo+gSoap+onvif学习总结:8、arm平台交叉编译运行及常见问题总结
Learning Tai Chi Maker - mqtt Chapter 2 (V) heartbeat mechanism
2022年安全员-A证考试题库及模拟考试
Huawei's 9-year experience as a software testing director
Light collector, Yunnan Baiyao!
!‘cat‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
Congratulations to myself, official account has more than ten thousand fans
Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems