当前位置:网站首页>力扣每日一题-第17天-349.两个数据的交集
力扣每日一题-第17天-349.两个数据的交集
2022-06-10 02:14:00 【重邮研究森】
2022.6.9今天你刷题了吗?
题目:
给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。
分析:
给定两个数组,找出里面具有相同的元素。
对于相同,也就是重复元素,哈希是个好方法!所以我们的思路是先把两个数组各种插入到另外一个map中,然后把重复元素去掉(这里也可以用set),然后再把这两个map插入到 另外一个map中,再次判断最后一个map中键值对为2的,那么就是重复元素。
解析:
1.哈希暴力
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
map<int, int>m1;
map<int, int>m2;
unordered_map<int, int>m3;
vector<int>v1;
for (auto num : nums1)
{
if (m1[num])
{
}
else
{
m1[num]++;
m3[num]++;
}
}
for (auto num : nums2)
{
if (m2[num])
{
}
else
{
m2[num]++;
m3[num]++;
}
}
for (auto num : m3)
{
if (num.second == 2)
{
v1.push_back(num.first);
}
}
return v1;
}
};2.哈希优化
这里我们先把一个数组处理存入map中,然后判断另外一个数组的数是否存在该map中
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
std::unordered_map<int, int> map;
vector<int> ans;
for (int i = 0; i < nums1.size(); i++) {
map[nums1[i]] = 1;
}
for (int i = 0; i < nums2.size(); i++) {
if (map[nums2[i]] == 1) {
//因为没有排序,所以重复了要清除
map[nums2[i]] = 0;
ans.push_back(nums2[i]);
}
}
return ans;
}
};
边栏推荐
- ZMQ communication
- [group planning final review iv] input / output system
- pycharm中numpy的安装与使用
- Organic metal porous material MOF (FE) contains camptothecin, and gambogic acid and indocyanine green | rmof-3 contain paclitaxel
- MySQL installation + test connection + common terminal MySQL commands (super detailed)
- Detailed explanation of equipment interaction API for dry goods 𞓜 app automated testing
- VirtualBox virtual machine network card settings
- Environment configuration and initial compilation of source code analysis of virtual engine ue4.26
- Programming Examples Using IBV Verbs
- Pat (advanced level) practice 1001 a+b format (Analog)
猜你喜欢

2、自然語言處理入門

程序员日常开发的八荣八耻

Operation of simulated examination platform of recurrent training question bank for operation of refrigeration and air conditioning equipment in 2022

CSP 202112-1 sequence query (detailed explanation)

副业收入是我做程序员的3倍,工作外的B面人生是怎样的?

Two software test questions

wps 调整文字间距

The starting salary is 1800 yuan. In the past three years, I have only saved 10000 yuan, and the monthly salary of leaving naked to go to the first tier cities is 30000 yuan

餐饮电子采购方案:采购全周期管理,节省30%成本

"Unable to access you may not have permission to use network resources" solution
随机推荐
CSP 202104-1 灰度直方图
35岁职场焦虑,越来越多的人这样选择
LABVIEW_ Classroom notes random (XIII) sub VI
鸡兔同笼问题
[group planning final review iv] input / output system
[win] the browser cannot open the web page and DNS appears_ PROBE_ FINISHED_ NO_ INTERNET
SSM framework integration - build a simple account login system
LABVIEW_课堂笔记 随机(十三)子VI
【WIN】浏览器打不开网页出现DNS_PROBE_FINISHED_NO_INTERNET
Hongmeng hilog log printing tips
重塑 IT 领导职业生涯的六种方法
JS sequence to obtain the distance between two places
Pat (advanced level) practice 1001 a+b format (Analog)
10 common high-frequency business scenarios that trigger IO bottlenecks
[FPGA] day16 FIFO implements UART protocol
機智雲輕網關服務,提昇生產管理效率
51 single chip microcomputer learning record (VI) IIC, ADC part
Web服务器端技术试题库
Simulated 100 questions and answers of the third batch of Guangdong Provincial Safety Officer a certificate (principal) examination in 2022
[experience sharing] summary of database operations commonly used in Django development