当前位置:网站首页>[OJ] intersection of two arrays (set, hash mapping...)
[OJ] intersection of two arrays (set, hash mapping...)
2022-07-02 23:36:00 【CodeWinter】
List of articles
OJ - Intersection of two arrays
questions : Simple
OJ link :349. Intersection of two arrays - Power button (LeetCode) (leetcode-cn.com)
Title Description :
Given two arrays nums1 and nums2 , Return their intersection . Every element in the output must be only Of . We can ignore the order of output results .
Example 1:
Input :nums1 = [1,2,2,1], nums2 = [2,2]
Output :[2]
Example 2:
Input :nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output :[9,4]
explain :[4,9] It is also passable
Solution 1 :set duplicate removal
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
// use unordered_set De duplication of elements in two arrays
unordered_set<int> s1(nums1.begin(), nums1.end());
unordered_set<int> s2(nums2.begin(), nums2.end());
// Traverse s1, In turn, it's s2 Find if there is s1 The elements in
for (auto& e : s1) {
if (s2.find(e) != s2.end()) res.push_back(e);
}
return res;
}
};
Solution 2 : Hash map
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
// hold nums1 All elements are mapped to the hash table
int hash[1001] = {
0 };
for (auto& e : nums1) hash[e]++;
// Traverse nums2, Check the hash table for nums2 The elements in , If hash[i] Greater than 0, Intersection
for (auto& e : nums2) {
if (hash[e] > 0) {
res.push_back(e);
hash[e] = 0; // Zeroing , prevent nums2 There are repeating elements behind , such as [9,4,9]
}
}
return res;
}
};
Solution 3 : Double pointer after sorting
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
// use set An array nums1 and nums2 De duplication and sorting
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 */
// Double pointer
auto it1 = s1.begin(), it2 = s2.begin();
// Traverse s1 and s2
while (it1 != s1.end() && it2 != s2.end()) {
// It didn't come to an end
// If the elements pointed to by two iterators are not equal , The small one goes forward ++
if (*it1 > *it2) it2++;
else if (*it1 < *it2) it1++;
else {
res.push_back(*it1); // intersection
it1++, it2++;
}
}
return res;
}
};
边栏推荐
- JSON data transfer parameters
- CDN 加速,需要域名先备案
- Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
- 第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
- Call vs2015 with MATLAB to compile vs Project
- [Verilog tutorial]
- Troubleshooting the cause of the crash when STM32 serial port dam receives 253 bytes
- SharedPreferences 保存List<Bean> 到本地并解决com.google.gson.internal.LinkedTreeMap cannot be cast to异常
- Connexion à distance de la tarte aux framboises en mode visionneur VNC
- VIM interval deletion note
猜你喜欢

【STL源码剖析】仿函数(待补充)

CDN 加速,需要域名先备案

数据集-故障诊断:西储大学轴承的各项数据以及数据说明

Hisilicon VI access video process

What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail

Implementation of VGA protocol based on FPGA

Wechat applet basic learning (wxss)

How does win11 turn on visual control? Win11 method of turning on visual control

Why does RTOS system use MPU?

What can I do after buying a domain name?
随机推荐
Why can't the start method be called repeatedly? But the run method can?
Submit code process
Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
Go basic constant definition and use
Dishes launcher small green program and directory management (efficiency tool)
Mapper代理开发
解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
购买完域名之后能干什么事儿?
Difference between NVIDIA n card and amda card
潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)
How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!
@How to use bindsinstance in dagger2
返回二叉树中最大的二叉搜索子树的根节点
(毒刺)利用Pystinger Socks4上线不出网主机
BBR encounters cubic
Win11系统explorer频繁卡死无响应的三种解决方法
内网渗透 | 手把手教你如何进行内网渗透
Arduino - 字符判断函数
Interface switching based on pyqt5 toolbar button -1
Agnosticism and practice makes perfect