当前位置:网站首页>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++;
}
}
}
};
边栏推荐
- Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
- ITK Example
- LeetCode 练习——206. 反转链表
- 漫画:有趣的海盗问题 (完整版)
- Cartoon: looking for the best time to buy and sell stocks
- 中国银河证券开户安全吗 开户后多久能买股票
- 2022新版PMP考试有哪些变化?
- 每日一练:关于日期的一系列
- Cloud security daily 220705: the red hat PHP interpreter has found a vulnerability of executing arbitrary code, which needs to be upgraded as soon as possible
- Independent development is a way out for programmers
猜你喜欢
CVPR 2022 best student paper: single image estimation object pose estimation in 3D space
企业数字化发展中的六个安全陋习,每一个都很危险!
Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
VBA drives SAP GUI to realize office automation (II): judge whether elements exist
leetcode每日一题:字符串中的第一个唯一字符
Redis基础
Compter le temps d'exécution du programme PHP et définir le temps d'exécution maximum de PHP
Thesis reading_ Medical NLP model_ EMBERT
Abnormal recovery of virtual machine Oracle -- Xi Fenfei
提高应用程序性能的7个DevOps实践
随机推荐
Ant financial's sudden wealth has not yet begun, but the myth of zoom continues!
MATLAB查阅
Tita 绩效宝:如何为年中考核做准备?
統計php程序運行時間及設置PHP最長運行時間
Simple query cost estimation
Anaconda中配置PyTorch环境——win10系统(小白包会)
Kafaka技术第一课
漫画:有趣的海盗问题 (完整版)
CVPR 2022 best student paper: single image estimation object pose estimation in 3D space
Data access - entityframework integration
Knowledge points of MySQL (7)
Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
证券网上开户安全吗?证券融资利率一般是多少?
QT console printout
Short the command line via jar manifest or via a classpath file and rerun
Force deduction solution summary 1200 minimum absolute difference
中国银河证券开户安全吗 开户后多久能买股票
Cmake tutorial Step3 (requirements for adding libraries)
Ten capabilities that cyber threat analysts should have
Read the history of it development in one breath