当前位置:网站首页>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++;
}
}
}
};
边栏推荐
- Oracle recovery tools -- Oracle database recovery tool
- Is it safe to open an account online? What is the general interest rate of securities financing?
- 2022 information system management engineer examination outline
- 这个17岁的黑客天才,破解了第一代iPhone!
- leetcode每日一练:旋转数组
- Humi analysis: the integrated application of industrial Internet identity analysis and enterprise information system
- Use QT designer interface class to create two interfaces, and switch from interface 1 to interface 2 by pressing the key
- Accuracy of BigDecimal Division
- Cartoon: looking for the k-th element of an unordered array (Revised)
- LeetCode 练习——206. 反转链表
猜你喜欢
leetcode每日一练:旋转数组
北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
EPM相关
求解为啥all(())是True, 而any(())是FALSE?
Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions
云主机oracle异常恢复----惜分飞
PMP认证需具备哪些条件啊?费用多少啊?
2022新版PMP考试有哪些变化?
LeetCode每日一题:合并两个有序数组
ELK日志分析系统
随机推荐
Why is all (()) true and any (()) false?
如何修改mysql字段为自增长字段
十个顶级自动化和编排工具
tkinter窗口预加载
MATLAB查阅
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
漫画:如何实现大整数相乘?(整合版)
MySQL之知识点(七)
Size_t 是无符号的
This 17-year-old hacker genius cracked the first generation iPhone!
Tita performance treasure: how to prepare for the mid year examination?
LeetCode每日一题:合并两个有序数组
Read libco save and restore the on-site assembly code
Force deduction solution summary 729- my schedule I
漫画:一道数学题引发的血案
c#图文混合,以二进制方式写入数据库
Cmake tutorial Step3 (requirements for adding libraries)
Database design in multi tenant mode
Cartoon: looking for the best time to buy and sell stocks
Interpretation: how to deal with the current security problems faced by the Internet of things?