当前位置:网站首页>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++;
}
}
}
};

边栏推荐
- Operation before or after Teamcenter message registration
- 漫画:有趣的海盗问题 (完整版)
- Cartoon: a bloody case caused by a math problem
- Troubleshooting - about clip not found Visual Studio
- 漫画:如何实现大整数相乘?(上) 修订版
- Cartoon: interesting pirate problem (full version)
- 哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
- LeetCode每日一题:合并两个有序数组
- Ten capabilities that cyber threat analysts should have
- 北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
猜你喜欢

VBA drives SAP GUI to realize office automation (II): judge whether elements exist

ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声

「运维有小邓」用于云应用程序的单点登录解决方案

Winedt common shortcut key modify shortcut key latex compile button

漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)

leetcode每日一题:字符串中的第一个唯一字符

LeetCode每日一题:合并两个有序数组

论文阅读_医疗NLP模型_ EMBERT

Short the command line via jar manifest or via a classpath file and rerun

统计php程序运行时间及设置PHP最长运行时间
随机推荐
Ten capabilities that cyber threat analysts should have
较文心损失一点点性能提升很多
7 pratiques devops pour améliorer la performance des applications
外盘黄金哪个平台正规安全,怎么辨别?
Read the history of it development in one breath
漫画:有趣的【海盗】问题
每日一练:关于日期的一系列
Tita 绩效宝:如何为年中考核做准备?
漫画:寻找股票买入卖出的最佳时机
Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
神经网络自我认知模型
Action avant ou après l'enregistrement du message teamcenter
Check the WiFi password connected to your computer
数据访问 - EntityFramework集成
Use QT designer interface class to create two interfaces, and switch from interface 1 to interface 2 by pressing the key
Anaconda中配置PyTorch环境——win10系统(小白包会)
2022年信息系统管理工程师考试大纲
LeetCode 练习——206. 反转链表
Cartoon: looking for the best time to buy and sell stocks
How to save the trained neural network model (pytorch version)