当前位置:网站首页>力扣每日一题-第18天-350.两个数据的交集Ⅱ
力扣每日一题-第18天-350.两个数据的交集Ⅱ
2022-06-10 14:44:00 【重邮研究森】
2022.6.10今天你刷题了吗?
题目:
给你两个整数数组 nums1 和 nums2 ,请你以数组形式返回两数组的交集。返回结果中每个元素出现的次数,应与元素在两个数组中都出现的次数一致(如果出现次数不一致,则考虑取较小值)。可以不考虑输出结果的顺序。
分析:
在给定的两个数组中,求出这两个数组的交集,存在的情况可能如下:
num1【】=【1,1,2,3】 nums2【】=【1,1,1,4,5】
结果:【1,1】
num1【】=【1,1,1,1,2,3】 nums2【】=【1,1,1,4,5】
结果:【1,1,1】
num1【】=【1,2,3】 nums2【】=【1,1,1,2,3】
结果:【1,2,3】
因此本题思路在于,先把一个nums放进map里面,并且记录每个值的次数,接下来我们遍历另外一个数组,并且判断这些数在map里面是否存在,如果存在,则存进vector里面,并且次数-1
这里-1操作是保证上面第一种,第二种情况的出现,通过减-1,可以保证取到最少的重复次数
解析:
1.哈希表
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int, int>map1;
//unordered_map<int, int>map2;
vector<int>v1;
for (auto num : nums1)
{
map1[num]++;
}
for (auto num : nums2)
{
// map2[num]++;
if (map1[num] !=0)
{
map1[num]--;
v1.push_back(num);
}
}
return v1;
}
};2.双指针
这里的思路是,利用两个指针分别指向两个数组,如果1指针的值小于2指针的值,那么1指针++,反之2指针++,当,1,2指针指向的值相等时,把该值插入vector中,然后1,2,指针都++
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
vector<int>vec;
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
int length1 = nums1.size();
int length2 = nums2.size();
int index1 = 0;
int index2 = 0;
while (index1 < length1 && index2 < length2)
{
if (nums1[index1] < nums2[index2])
{
index1++;
}
else if (nums1[index1] > nums2[index2])
{
index2++;
}
else
{
vec.push_back(nums1[index1]);
index1++;
index2++;
}
}
return vec;
}
};边栏推荐
- 数据湖(六):Hudi与Flink整合
- Does Fortran have a standard library
- 如何实现erp外网连接?
- [discrete mathematics review series] v. some special charts
- 【LogoDetection 数据集处理】(2)画出训练集图片的标注框
- Ue5 how to convert screen coordinates to world coordinates and World Directions
- Wechat applet date comparison, calculation days
- 欧几里得算法求最大公因数 Go语言实现
- CVPR 2022 Oral | SCI:实现快速、灵活与稳健的低光照图像增强
- [logodetection data set processing] (3) divide the training set into multiple folders by category
猜你喜欢

【离散数学期复习系列】三、集合的概念及运算
![[discrete mathematics review series] IV. figure](/img/70/5b2f783265e7e5f6485b49088400da.png)
[discrete mathematics review series] IV. figure

WordPress的管理员用户名是如何泄露的

CVPR 2022 | 基于序列对比学习的长视频逐帧动作表示

C multithreading learning note 2

Do you understand all these difficult memory problems?

Basic concept of data warehouse

2022 Shandong Province safety officer C certificate retraining question bank and online simulation examination

How to implement the association between interfaces in JMeter?

超强实操!手把手教学Kinect深度图与RGB摄像头的标定与配准
随机推荐
Operation of simulated examination platform for theoretical question bank of refrigeration and air conditioning equipment operation in 2022
js获取数组中最大值
CG碰撞检测 Collision Testing
LeetCode_20(括号匹配)
[discrete mathematics review series] v. some special charts
【離散數學期複習系列】二、一階邏輯(謂詞邏輯)
LeetCode_ 21 (merge two ordered linked lists)
数仓的基本概念
数据湖(六):Hudi与Flink整合
【离散数学期复习系列】一、命题逻辑
Consumption mode of Message Oriented Middleware
C multithreading learning note 4
Does Fortran have a standard library
【离散数学期复习系列】六、树
[logodetection data set processing] (1) divide the data set into training set and verification set
UE5如何將屏幕坐標轉為世界坐標和世界方向
Gin blog summary 1
Anaconda installs opencv (CV2) and uses it in the jupyter notebook
Design tools and skills for beginners to build their own blog
LeetCode_21(合并两个有序链表)