当前位置:网站首页>[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;
}
};
边栏推荐
- Intranet penetration | teach you how to conduct intranet penetration hand in hand
- 非路由组件之头部组件和底部组件书写
- Difference between NVIDIA n card and amda card
- 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
- Optimization of streaming media technology
- golang中new与make的区别
- Convolution和Batch normalization的融合
- PR FAQ, what about PR preview video card?
- How to apply for company email when registering in company email format?
猜你喜欢

跨境电商如何通过打好数据底座,实现低成本稳步增长

C MVC creates a view to get rid of the influence of layout

JSON数据传递参数

The use of 8255 interface chip and ADC0809

Dishes launcher small green program and directory management (efficiency tool)

潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)

Flexible combination of applications is a false proposition that has existed for 40 years

采用VNC Viewer方式遠程連接樹莓派

Convolution和Batch normalization的融合

Where is the win11 microphone test? Win11 method of testing microphone
随机推荐
CDN 加速,需要域名先备案
判断二叉树是否为满二叉树
C MVC creates a view to get rid of the influence of layout
万物并作,吾以观复|OceanBase 政企行业实践
How to set automatic reply for mailbox and enterprise mailbox?
YOLOX加强特征提取网络Panet分析
Implementation of VGA protocol based on FPGA
内网渗透 | 手把手教你如何进行内网渗透
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
Catalogue of digital image processing experiments
C# MVC创建一个视图摆脱布局的影响
Warning: implicitly declaring library function 'printf' with type 'int (const char *,...)‘
Three solutions to frequent sticking and no response of explorer in win11 system
Win11系统explorer频繁卡死无响应的三种解决方法
@How to use bindsinstance in dagger2
Intranet penetration | teach you how to conduct intranet penetration hand in hand
RuntimeError: no valid convolution algorithms available in CuDNN
Go basic constant definition and use
Eight honors and eight disgraces of the programmer version~
sourcetree 详细