当前位置:网站首页>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);
}
}
边栏推荐
- 穿越封锁的最新利器,速度最快梯没有之一。
- 109. 简易聊天室12:实现客户端一对一聊天
- Learn Taiji Maker - mqtt Chapter 2 (IV) esp8266 reserved message application
- 整理网上蛋糕商城项目
- Notepad++ -- common plug-ins
- 2022西式面点师(高级)考试试题模拟考试平台操作
- 2022年安全员-B证考试题库及答案
- lotus v1.16.0 calibnet
- Severe tire damage: the first rock band in the world to broadcast live on the Internet
- 2022电力电缆判断题模拟考试平台操作
猜你喜欢

Learning Tai Chi Maker - mqtt Chapter II (VI) mqtt wills

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

学习太极创客 — MQTT 第二章(六)MQTT 遗嘱

Performance optimization and implementation of video codec

论文详读:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION

mysql----where 1=1是什么意思

S32ds jump to defaultisr

OracleData安装问题

2022 high altitude installation, maintenance and removal examination questions and answers

Sword finger offer 53 - I. find the number I in the sorted array (improved bisection)
随机推荐
代码理解:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION
2022年G3锅炉水处理复训题库模拟考试平台操作
恭喜我自己,公众号粉丝破万
On the necessity of building a video surveillance convergence platform and its scenario application
Learning Tai Chi Maker - mqtt Chapter II (VI) mqtt wills
[csp-j2020] excellent splitting
The short video local life section has become popular. How to grasp the new opportunities?
What to do when MySQL changes the password and reports an error
Necessary skills for test and development: actual combat of security test vulnerability shooting range
Learning Tai Chi Maker - mqtt Chapter 2 (V) heartbeat mechanism
程序员-放羊娃
Sword finger offer 47 Maximum gift value (DP)
Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
活性染料研究:Lumiprobe AF594 NHS 酯,5-异构体
2022年安全员-B证考试题库及答案
mysql----where 1=1是什么意思
无线传感器网络学习笔记(一)
高通平台 Camera 之 MCLK 配置
基于微信小程序的婚纱影楼门户小程序
DH parameters of robotics and derivation using MATLAB symbolic operation