当前位置:网站首页>【OJ】两个数组的交集(set、哈希映射 ...)
【OJ】两个数组的交集(set、哈希映射 ...)
2022-07-02 22:17:00 【CodeWinter】
OJ - 两个数组的交集
题目难度:简单
OJ链接:349. 两个数组的交集 - 力扣(LeetCode) (leetcode-cn.com)
题目描述:
给定两个数组 nums1 和 nums2 ,返回它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以不考虑输出结果的顺序 。
示例 1:
输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2]
示例 2:
输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[9,4]
解释:[4,9] 也是可通过的
解法一:set去重
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
// 用unordered_set对两个数组中的元素去重
unordered_set<int> s1(nums1.begin(), nums1.end());
unordered_set<int> s2(nums2.begin(), nums2.end());
// 遍历s1,依次在s2中查找是否有s1中的元素
for (auto& e : s1) {
if (s2.find(e) != s2.end()) res.push_back(e);
}
return res;
}
};
解法二:哈希映射
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
// 把nums1所有元素映射到哈希表中
int hash[1001] = {
0 };
for (auto& e : nums1) hash[e]++;
// 遍历nums2,在哈希表中查找是否有nums2中的元素,如果hash[i]大于0,即为交集
for (auto& e : nums2) {
if (hash[e] > 0) {
res.push_back(e);
hash[e] = 0; // 置零,防止nums2后面有重复元素,比如[9,4,9]
}
}
return res;
}
};
解法三:排序后双指针
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
// 用set对数组nums1和nums2进行去重和排序
set<int> s1(nums1.begin(), nums1.end());
set<int> s2(nums2.begin(), nums2.end());
/* nums1 = [4,9,5], nums2 = [9,4,9,8,4] * s1 = 4,5,9 * s2 = 4,8,9 */
// 双指针
auto it1 = s1.begin(), it2 = s2.begin();
// 遍历s1和s2
while (it1 != s1.end() && it2 != s2.end()) {
// 都没走到头
// 如果两迭代器指向的元素不相等,小的往前++
if (*it1 > *it2) it2++;
else if (*it1 < *it2) it1++;
else {
res.push_back(*it1); // 交集
it1++, it2++;
}
}
return res;
}
};
边栏推荐
- 高数有多难?AI 卷到数学圈,高数考试正确率 81%!
- PotPlayer设置最小化的快捷键
- All things work together, and I will review oceanbase's practice in government and enterprise industry
- Use redis to realize self increment serial number
- How to maintain the brand influence of clothing enterprises
- MySQL基础
- Pandora IOT development board learning (HAL Library) - Experiment 4 serial port communication experiment (learning notes)
- Highly available cluster (HAC)
- [Verilog tutorial]
- Go basic data type
猜你喜欢

RecyclerView结合ViewBinding的使用

Go project operation method

Bean加载控制

Yolox enhanced feature extraction network panet analysis

Go basic data type

Go basic anonymous variable

解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
![[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)](/img/75/3f6203410dd2754e578e0baaaa9aef.png)
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)

Convolution和Batch normalization的融合

Speech recognition Series 1: speech recognition overview
随机推荐
Redis expiration policy +conf record
Arduino - 字符判断函数
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
Ideal car × Oceanbase: when the new forces of car building meet the new forces of database
请求与响应
Mapper代理开发
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
基于FPGA的VGA协议实现
【直播预约】数据库OBCP认证全面升级公开课
Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
MarkDown基本语法
Win11系统explorer频繁卡死无响应的三种解决方法
Speech recognition Series 1: speech recognition overview
Win11麦克风测试在哪里?Win11测试麦克风的方法
基于Pyqt5工具栏按钮可实现界面切换-2
Quantitative analysis of PSNR, SSIM and RMSE
Go basic anonymous variable
Agnosticism and practice makes perfect
Cryptographic technology -- key and ssl/tls
Win11自动关机设置在哪?Win11设置自动关机的两种方法