当前位置:网站首页>力扣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());
}
};边栏推荐
- 【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey
- Singapore parent-child tour, these popular attractions must be arranged
- Pourquoi l'ordinateur n'a - t - il pas de réseau après l'ouverture du Hotspot win10?
- Using the command line to convert JSON to dart file in fluent
- Threadlocal
- BeanFactory创建流程
- Input / output and interrupt technology -- microcomputer Chapter 6 learning notes
- The subsystem implementing transaction persistence in DBMS is ()
- harbor api 2.0查询
- [UAV] kinematic analysis from single propeller to four rotor UAV
猜你喜欢
![[FPGA] IIC读写EEPROM 的实现](/img/6a/36e9355058a90d98cffafcbaa1930b.png)
[FPGA] IIC读写EEPROM 的实现

Dual domain SSL certificate

Input / output and interrupt technology -- microcomputer Chapter 6 learning notes

力扣589:N 叉树的前序遍历

BeanFactory创建流程

MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field

Collective system

Yolov5 torch installation

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

Learning about signals
随机推荐
Servlet lifecycle
Method of applying for code signing certificate by enterprise
Qos(Quality of Service)
Bean创建流程 与 lazy-init 延迟加载机制原理
Yolov5 torch installation
[fpga] implementation of IIC read / write EEPROM
[UGV] schematic diagram of UGV version 32
Process architecture and process management
PBR material: basic principle and simple fabrication
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
為什麼win10開熱點後電腦沒有網絡?
Deep learning ----- different methods to realize inception-10
Array of small C
Websocket implementation principle
IO stream, character read / write, copy
Input / output and interrupt technology -- microcomputer Chapter 6 learning notes
Singapore parent-child tour, these popular attractions must be arranged
How to repair expired SSL certificates?
Equity interest [non DP]
Network high concurrency