当前位置:网站首页>力扣349. 两个数组的交集
力扣349. 两个数组的交集
2022-06-30 04:45:00 【智慧的人不要秃头】
给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。

需要特别注意:输出结果中的每个元素一定是唯一的,也就是输出的结果是去重的,同时顺序是不需要考虑的
使用数组来做哈希的题目,是因为题目限制了数值的大小,而这道题目没有限制每个元素出现的次数,所以可以不用数组。
而且如果哈希值比较少,特别分散、跨度非常大,使用数组就会造成空间的极大浪费。
此时就要使用另一种结构体了,set ,关于set,C++ 给提供了如下三种可用的数据结构:
- std::set
- std::multiset
- std::unordered_set
std::set和std::multiset底层实现都是红黑树,std::unordered_set的底层实现是哈希表, 使用unordered_set 读写效率是最高的,并不需要对数据进行排序,而且还不要让数据重复,所以选择unordered_set。

思路如图所示:

方法:unordered_set
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
//存放返回的结果
unordered_set<int> result_set;
unordered_set<int> nums_set(nums1.begin(), nums1.end());
for(int num : nums2) {
//发现nums2中的元素在nums1中存在,就记录该元素到返回结果集中
if(nums_set.find(num) != nums_set.end()) {
result_set.insert(num);
}
}
//注意这里需要返回的是数组
return vector<int>(result_set.begin(), result_set.end());
}
};边栏推荐
- MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值
- Wildcard SSL certificate issuing time
- 为什么win10开热点后电脑没有网络?
- Universal Studios Singapore: a good place for a one-day parent-child tour in Singapore
- Redis实现短信登入功能(二)Redis实现登入功能
- Create a simple battle game with photon pun
- Recommended cultural landmarks of these tourist attractions in Bangkok
- Webots learning notes
- Arrays class
- MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field
猜你喜欢

What is multimodal interaction?

How to renew an SSL certificate
![[UGV] schematic diagram of UGV version 32](/img/4b/03471d2cc96be5d57c97fd1c4e17dc.jpg)
[UGV] schematic diagram of UGV version 32

【Paper】2016_ A Learning-Based Fault Tolerant Tracking Control of an Unmanned Quadrotor Helicopter

Introduction to some representations, neighbors and degrees of Graphs

Singapore must visit these scenic spots during the Spring Festival

Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium

史上最全的Redis基础+进阶项目实战总结笔记

Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge

【Paper】2013_ An efficient model predictive control scheme for an unmanned quadrotor helicopter
随机推荐
破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
Dual domain SSL certificate
Have a heart beating Valentine's day in Singapore
圆心科技,很焦虑?
Wildcard SSL certificate issuing time
【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey
Issue SSL certificate with IP address
Difference between request forwarding and redirection
Unreal 4 learning notes - data storage using blueprints
On mask culling of unity
力扣2049:统计最高分的节点数目
Difference between TCP three handshakes and four waves and tcp/udp
Differences between cookies and sessions
Enlist soldiers and generals, draw small programs, multi-threaded display time
Directory operations and virtual file systems
Paging query, using jdbc-- paging query
Array of small C
【Paper】2019_ Distributed Cooperative Control of a High-speed Train
Universal Studios Singapore: a good place for a one-day parent-child tour in Singapore
Equity interest [non DP]