当前位置:网站首页>力扣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());
}
};边栏推荐
- BeanFactory创建流程
- Difference between TCP three handshakes and four waves and tcp/udp
- Create a simple battle game with photon pun
- Redis实现短信登入功能(一)传统的Session登入
- Network high concurrency
- Redis implements SMS login function (I) traditional session login
- Differences between beanfactory and factorybean
- [UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example
- 【Paper】2019_ Distributed Cooperative Control of a High-speed Train
- Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)
猜你喜欢

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
![[UAV] kinematic analysis from single propeller to four rotor UAV](/img/32/1a88b102f832ffbbc1a7e57798260a.jpg)
[UAV] kinematic analysis from single propeller to four rotor UAV

IIS request SSL certificate

Singapore must visit these scenic spots during the Spring Festival

Intern method of string

Deep learning ----- different methods to realize inception-10

【Paper】2019_ Consensus Control of Multiple AUVs Recovery System Under Switching Topologies and Time D

BeanFactory创建流程

Royal Albert Hall, a popular landmark in London
随机推荐
Using the command line to convert JSON to dart file in fluent
Enter the date format string as the production date of the commodity, and enter the shelf life (days); Calculate the number of days until today before the expiration date of the product. 1. Change the
【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV
为什么win10开热点后电脑没有网络?
Wildcard SSL certificate issuing time
Static keyword
【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey
Difference between TCP three handshakes and four waves and tcp/udp
Is the Flink connector JDBC open source? Where can I download it
Bean creation process and lazy init delay loading mechanism
Royal Albert Hall, a popular landmark in London
Mongodb learning
Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)
One interview question every day to talk about the process of TCP connection and disconnection
Qos(Quality of Service)
File system and directory operations
Thread safety and processing caused by multithreading
Redis implements SMS login function (II) redis implements login function
【Paper】2019_ Distributed Cooperative Control of a High-speed Train
[control] multi agent system summary. 4. control agreement.