当前位置:网站首页>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++;
}
}
}
};
边栏推荐
- 哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
- 得知女儿被猥亵,35岁男子将对方打至轻伤二级,法院作出不起诉决定
- 蚂蚁金服的暴富还未开始,Zoom的神话却仍在继续!
- Domain name resolution, reverse domain name resolution nbtstat
- 解决“双击pdf文件,弹出”请安装evernote程序
- Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
- Short the command line via jar manifest or via a classpath file and rerun
- 漫画:有趣的【海盗】问题
- 提高应用程序性能的7个DevOps实践
- 删除数组中的某几个元素
猜你喜欢
Kafaka technology lesson 1
MySQL之知识点(六)
Kafaka技术第一课
Knowledge points of MySQL (7)
Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
C # mixed graphics and text, written to the database in binary mode
EPM相关
Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
企业数字化发展中的六个安全陋习,每一个都很危险!
mongodb(快速上手)(一)
随机推荐
BigDecimal除法的精度问题
排错-关于clion not found visual studio 的问题
How to modify MySQL fields as self growing fields
提高應用程序性能的7個DevOps實踐
Cartoon: how to multiply large integers? (next)
基于YOLOv3的口罩佩戴检测
Tita 绩效宝:如何为年中考核做准备?
Server configuration jupyter environment
IDEA 项目启动报错 Shorten the command line via JAR manifest or via a classpath file and rerun.
Independent development is a way out for programmers
EPM相关
漫画:有趣的海盗问题 (完整版)
Simple query cost estimation
Please tell me why some tables can find data by writing SQL, but they can't be found in the data map, and the table structure can't be found
Kafaka technology lesson 1
MATLAB查阅
The comprehensive competitiveness of Huawei cloud native containers ranks first in China!
[binary tree] insufficient nodes on the root to leaf path
世界上最难的5种编程语言
中国银河证券开户安全吗 开户后多久能买股票