当前位置:网站首页>LeetCode 88:合并两个有序数组
LeetCode 88:合并两个有序数组
2022-06-28 04:14:00 【斯沃福德】
题目:
注意数组默认已经是升序。
方法一:
使用双指针p1 p2遍历两个数组;
过程和合并两个排序的链表类似
当p1 p2还未遍历到头,则从前向后判断p1 p2指针所指的元素,小的就放入r 数组;
当有任一数组到头了,则跳出循环,将未到头的那个数组继续添加至r 数组即可;
最后拷贝至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++];
}
}
//任一数组到头了
while(p1<m){
r[curr++]=nums1[p1++];
}
while(p2<n){
r[curr++]=nums2[p2++];
}
System.arraycopy(r,0,nums1,0,n+m);
}
}
方法二:Arrays.sort
先将nums2直接添加进nums1,然后使用Java自带API进行排序
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);
}
}
边栏推荐
- Secouer le son et se battre ~ prêter attention au blogueur
- Mise en place d'un cadre d'essai d'automatisation de l'interface utilisateur - - rédaction d'une application d'automatisation
- Taco: a data enhancement technique for character recognition
- Live online source code, JS dynamic effect, sidebar scrolling fixed effect
- 【Proteus仿真】定时器1外部计数中断
- CUPTI error: CUPTI could not be loaded or symbol could not be found.
- Google Earth Engine(GEE)——全球洪水数据库 v1 (2000-2018年)
- Go language learning tutorial (14)
- 成长一夏 挑战赛来袭 | 学习、创作两大赛道,开启导师报名啦!
- E-week finance Q1 mobile banking has 650million active users; Layout of financial subsidiaries in emerging fields
猜你喜欢

UI automation test framework construction - write an app automation

Multithreading and high concurrency IV: varhandle, strong weak virtual reference and ThreadLocal

一文详解|增长那些事儿
![[applet] solution document using font awesome Font Icon (picture and text)](/img/1b/d1b738e6e35e59cc4a417df4ea0e8d.png)
[applet] solution document using font awesome Font Icon (picture and text)

UI自动化测试框架搭建 —— 编写一个APP自动化

JVM I: introduction to JVM and understanding of class files

Little knowledge about function templates --

云厂商为什么都在冲这个KPI?

100+ data science interview questions and answers Summary - machine learning and deep learning

MySQL gets the current date of the year
随机推荐
Multi project design and development · introduction to class library project
灵活的IP网络测试工具——— X-Launch
C语言全局变量(c文件和h文件中的全局变量、静态全局变量)使用注意事项
请问下,mysqlcdc设置多并行度的话,增量数据是不是会重复?
flinkcdc采集oracle,oracle数据库是CDB的
Introduction to SQLSERVER database
Digital promising, easy to reach, Huawei accelerates the layout of the commercial market with "five pole" star products
一文详解|增长那些事儿
mysql修改密码报错需要怎么做
2022年中国音频市场年度综合分析
Notepad++ -- column editing mode -- Usage / instance
27 years, Microsoft IE is over!
[Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]
leetcode:714. The best time to buy and sell stocks includes handling fee [DP dual status]
Aspnetcoreratelimit rate limit interface access limit current limit control
云厂商为什么都在冲这个KPI?
Multi thread implementation rewrites run (), how to inject and use mapper file to operate database
Database garbled
JS reverse massive star map sign signature
Password encryption MD5 and salt treatment