当前位置:网站首页>349. 两个数组的交集
349. 两个数组的交集
2022-07-26 10:42:00 【Forest_1010】

首先使用两个集合分别存储两个数组中的元素,然后遍历其中的一个集合,判断其中的每个元素是否在另一个集合中,如果元素也在另一个集合中,则将该元素添加到返回值。该方法的时间复杂度可以降低到 O(m+n)
集合分为三种:
- std::set
底层实现:红黑树
是否有序:有序
数值是否可以重复:否
查询效率:O(logn) - std::multiset
底层实现:红黑树
是否有序:有序
数值是否可以重复:是
查询效率:O(logn) - std::unordered_set
底层实现:哈希表
是否有序:无序
数值是否可以重复:否
查询效率:O(1)
综上,使用unordered_set效率是最高的。
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> result_set;
unordered_set<int> nums1_set(nums1.begin(), nums1.end());//将num1中的数据存入到nums1_set
unordered_set<int> nums2_set(nums2.begin(), nums2.end());//nums1_set,nums2_set中都没有重复的元素了
for (int num : nums2_set) //遍历nums2_set:从数组nums2依次取出元素赋值给整型变量nums,循环执行for中语句
{
//如果 存在:查找num是否存在,存在返回该元素的迭代器,不存在返回nums1_set.end();
if (nums1_set.find(num) != nums1_set.end()) //在集合1中查找,发现也有集合2中的这个元素
{
result_set.insert(num);
}
}
return vector<int>(result_set.begin(), result_set.end());
}
};
边栏推荐
- Sql Server之查询总结
- The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly
- 【小程序】onReachBottom 事件为什么不能触发 ?(一秒搞定)
- Phase 4: one of College Students' vocational skills preparation in advance
- [leetcode daily question 2021/4/23]368. Maximum divisible subset
- [leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]
- 使用float实现左中右布局,中间内容自适应
- .net operation redis hash object
- winpcap 抓包函数pcap_loop(),停止问题
- Flutter TextField设置高度并且自动换行,圆角边框去除下划线
猜你喜欢

【机器学习小记】【搭建循环神经网络及其应用】deeplearning.ai course5 1st week programming(keras)

RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
![[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array](/img/9b/624416fa6a408bf64ca5438273176b.png)
[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array
控制随机抽中几率 [ C# | Random ]

Issue 5: the second essential skill for College Students

IAR sprintf 浮点 在UCOS 总格式化成0.0的问题

【小程序】onReachBottom 事件为什么不能触发 ?(一秒搞定)

按二进制数中1的个数分类

344.反转字符串

【机器学习小记】【人脸识别】deeplearning.ai course4 4th week programming
随机推荐
Issue 5: the second essential skill for College Students
A semicolon is missing
Redis implementation of distributed lock solution
Successfully transplanted stemwin v5.22 on Shenzhou IV development board
Flutter 防止科学计数并去除尾数无效0
RT thread learning notes (III) -- building a compilation environment with scons
[leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits
使用float实现左中右布局,中间内容自适应
如何实现临时的图形要素现实
oracle 启动不了 tnslistener服务启动不了
剑指Offer(十):矩形覆盖
11 handle "self assignment" in operator=
[转]ArcGIS中判断两个Geometry之间的关系
Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2
[paper after dinner] deep mining external perfect data for chestx ray disease screening
Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
STM32 Alibaba cloud mqtt esp8266 at command
Issue 7: how do you choose between curling up and lying flat
flutter dart生成N个区间范围内不重复随机数List
Koin