当前位置:网站首页>Leetcode daily question: merge two ordered arrays
Leetcode daily question: merge two ordered arrays
2022-07-05 17:48:00 【Sharp blade CC】

link : Merge two ordered arrays
This question has a requirement : Can I use O(m+n) The time complexity of ?
Of course there are !
Ideas : Point to the tail of two arrays with two pointers ! This is the key !
Then traverse from back to front . You can know the title ,nums1 It must be the size of m+n Of , And nums1 The second half of the is empty , Direct coverage has no effect .
So it's going to be nums2 Elements in and nums1 The comparison in , Let whoever is old go in first .
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int p1=m-1,p2=n-1;
int i=1;
while(p1>=0||p2>=0)
{
if(p1<0)
{
nums1[m+n-i]=nums2[p2];
p2--;
i++;
}
else if(p2<0)
{
break;
}
else if(nums1[p1]<=nums2[p2])
{
nums1[m+n-i]=nums2[p2];
i++;
p2--;
}
else
{
nums1[m+n-i]=nums1[p1];
p1--;
i++;
}
}
}
};

边栏推荐
- ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
- 忽米沄析:工业互联网标识解析与企业信息系统的融合应用
- PMP认证需具备哪些条件啊?费用多少啊?
- Ten top automation and orchestration tools
- Knowing that his daughter was molested, the 35 year old man beat the other party to minor injury level 2, and the court decided not to sue
- SQL Server(2)
- Kafaka技术第一课
- leetcode每日一练:旋转数组
- Short the command line via jar manifest or via a classpath file and rerun
- 排错-关于clion not found visual studio 的问题
猜你喜欢

MySQL之知识点(七)

Knowledge points of MySQL (7)

MATLAB查阅

7 pratiques devops pour améliorer la performance des applications

Cmake tutorial Step4 (installation and testing)

「运维有小邓」用于云应用程序的单点登录解决方案
Database design in multi tenant mode

Oracle recovery tools -- Oracle database recovery tool

ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise

每日一练:关于日期的一系列
随机推荐
C # mixed graphics and text, written to the database in binary mode
Clickhouse (03) how to install and deploy Clickhouse
Ordinary programmers look at the code, and top programmers look at the trend
数据访问 - EntityFramework集成
Ant financial's sudden wealth has not yet begun, but the myth of zoom continues!
这个17岁的黑客天才,破解了第一代iPhone!
请问下为啥有的表写sql能查到数据,但在数据地图里查不到啊,查表结构也搜不到
每日一练:关于日期的一系列
MySQL之知识点(六)
Mysql5.6 parsing JSON strings (supporting complex nested formats)
Matlab reference
7 pratiques devops pour améliorer la performance des applications
Which platform of outer disk gold is regular and safe, and how to distinguish it?
Flow characteristics of kitchen knife, ant sword, ice scorpion and Godzilla
外盘黄金哪个平台正规安全,怎么辨别?
Mongodb (quick start) (I)
QT控制台打印输出
Tita performance treasure: how to prepare for the mid year examination?
flask接口响应中的中文乱码(unicode)处理
如何保存训练好的神经网络模型(pytorch版本)