当前位置:网站首页>力扣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());
}
};边栏推荐
- Oculus quest2 development: (I) basic environment construction and guide package
- Webots notes day 2
- Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor
- National Museum of Singapore - give you spiritual and physical satisfaction
- Qos(Quality of Service)
- Deep learning ----- different methods to realize inception-10
- SCM learning notes: interrupt learning
- [fpga] implementation of IIC read / write EEPROM
- Royal Albert Hall, a popular landmark in London
- 深度学习------不同方法实现Inception-10
猜你喜欢

Why does the computer have no network after win10 is turned on?

SSL update method

Dual domain SSL certificate

【Paper】2021_ Observer-Based Controllers for Incrementally Quadratic Nonlinear Systems With Disturbanc

Network layer protocol hardware

Connect to the database and run node JS running database shows that the database is missing

This connection is not a private connection this website may be pretending to steal your personal or financial information

Redis implements SMS login function (I) traditional session login

A collection of errors encountered in machine learning with unity

Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)
随机推荐
EasyRecovery数据恢复软件 恢复了我两年前的照片视频数据
Redis实现短信登入功能(一)传统的Session登入
HTC vive cosmos development - handle button event
Detailed explanation of cookies and sessions
為什麼win10開熱點後電腦沒有網絡?
Singapore must visit these scenic spots during the Spring Festival
Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium
Software digital signature certificate
Websocket implementation principle
[control] multi agent system summary. 1. system model. 2. control objectives. 3. model transformation.
Free travel recommendation in Bangkok: introduction to the Mekong River in Bangkok
Static keyword
Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
Dual domain SSL certificate
Autowired注解警告的解决办法
How to repair expired SSL certificates?
IIS request SSL certificate
【Paper】2016_ A Learning-Based Fault Tolerant Tracking Control of an Unmanned Quadrotor Helicopter
【Paper】2021_ Analysis of the Consensus Protocol of Heterogeneous Agents with Time-Delays
SCM learning notes: interrupt learning