当前位置:网站首页>【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;
}
};
边栏推荐
- JDBC教程
- 内网渗透 | 手把手教你如何进行内网渗透
- 一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
- [analysis of STL source code] imitation function (to be supplemented)
- RuntimeError: no valid convolution algorithms available in CuDNN
- MySQL基础
- C#中Linq用法汇集
- [adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
- Submit code process
- 购买完域名之后能干什么事儿?
猜你喜欢
Go basic anonymous variable
LINQ usage collection in C #
What experience is there only one test in the company? Listen to what they say
Speech recognition Series 1: speech recognition overview
Golang common settings - modify background
C#中Linq用法汇集
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)
Win11系统explorer频繁卡死无响应的三种解决方法
What can I do after buying a domain name?
Dishes launcher small green program and directory management (efficiency tool)
随机推荐
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)
聊聊内存模型与内存序
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
[error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
面试过了,起薪16k
2022 latest and complete interview questions for software testing
YOLOX加强特征提取网络Panet分析
Explain promise usage in detail
CDN acceleration requires the domain name to be filed first
Load balancing cluster (LBC)
FOC矢量控制及BLDC控制中的端电压、相电压、线电压等概念别还傻傻分不清楚
Makefile configuration of Hisilicon calling interface
富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
20220524_ Database process_ Statement retention
Yolox enhanced feature extraction network panet analysis
【Redis笔记】压缩列表(ziplist)
采用VNC Viewer方式远程连接树莓派
Highly available cluster (HAC)
Brief introduction to common sense of Zhongtai
php 获取真实ip