当前位置:网站首页>[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;
}
};
边栏推荐
- Win11系统explorer频繁卡死无响应的三种解决方法
- @BindsInstance在Dagger2中怎么使用
- (毒刺)利用Pystinger Socks4上线不出网主机
- SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
- Convolution和Batch normalization的融合
- Intranet penetration | teach you how to conduct intranet penetration hand in hand
- Submit code process
- Win11自动关机设置在哪?Win11设置自动关机的两种方法
- Flexible combination of applications is a false proposition that has existed for 40 years
- Yolox enhanced feature extraction network panet analysis
猜你喜欢

Convolution和Batch normalization的融合
![[analysis of STL source code] imitation function (to be supplemented)](/img/40/a02a04a24f385a31e0484d1071ecec.jpg)
[analysis of STL source code] imitation function (to be supplemented)

YOLOX加强特征提取网络Panet分析

golang入门:for...range修改切片中元素的值的另类方法

How to set automatic reply for mailbox and enterprise mailbox?

Redis expiration policy +conf record

Markdown basic grammar

【ML】李宏毅三:梯度下降&分类(高斯分布)

Remote connection of raspberry pie by VNC viewer

采用VNC Viewer方式遠程連接樹莓派
随机推荐
Go project operation method
PR FAQ, what about PR preview video card?
Pandora IOT development board learning (HAL Library) - Experiment 4 serial port communication experiment (learning notes)
理想汽车×OceanBase:当造车新势力遇上数据库新势力
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
JSON data transfer parameters
(毒刺)利用Pystinger Socks4上线不出网主机
Optimization of streaming media technology
可知论与熟能生巧
C MVC creates a view to get rid of the influence of layout
"A good programmer is worth five ordinary programmers!"
Doorplate making C language
Win11自动关机设置在哪?Win11设置自动关机的两种方法
golang入门:for...range修改切片中元素的值的另类方法
数据集-故障诊断:西储大学轴承的各项数据以及数据说明
JDBC教程
Difference between NVIDIA n card and amda card
Convolution和Batch normalization的融合
How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base
Win11麦克风测试在哪里?Win11测试麦克风的方法