当前位置:网站首页>【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;
}
};
边栏推荐
- How to maintain the brand influence of clothing enterprises
- Use redis to realize self increment serial number
- Simple square wave generating circuit [51 single chip microcomputer and 8253a]
- Arduino - character judgment function
- vim区间删行注释
- How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base
- 2022 latest and complete interview questions for software testing
- Print out mode of go
- SQL advanced syntax
- 内网渗透 | 手把手教你如何进行内网渗透
猜你喜欢

Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决

RecyclerView结合ViewBinding的使用

JDBC練習案例

2022年最新最全软件测试面试题大全

非路由组件之头部组件和底部组件书写

PotPlayer设置最小化的快捷键

采用VNC Viewer方式远程连接树莓派

C#中Linq用法汇集

The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear

Dishes launcher small green program and directory management (efficiency tool)
随机推荐
基于Pyqt5工具栏按钮可实现界面切换-1
海思 VI接入视频流程
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
I've been interviewed. The starting salary is 16K
Troubleshooting the cause of the crash when STM32 serial port dam receives 253 bytes
Interface switching based on pyqt5 toolbar button -2
解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
Use of recyclerview with viewbinding
【ML】李宏毅三:梯度下降&分类(高斯分布)
Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)
Why can't the start method be called repeatedly? But the run method can?
How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base
What if win11 can't turn off the sticky key? The sticky key is cancelled but it doesn't work. How to solve it
Use the scroll bar of souI when using the real window in souI
Li Kou brush questions (2022-6-28)
A single element in an ordered array -- Valentine's Day mental problems
[live broadcast appointment] database obcp certification comprehensive upgrade open class
Pandora IOT development board learning (HAL Library) - Experiment 4 serial port communication experiment (learning notes)
CDN 加速,需要域名先备案