当前位置:网站首页>Li Kou daily question - day 18 -350 Intersection of two data Ⅱ
Li Kou daily question - day 18 -350 Intersection of two data Ⅱ
2022-06-10 15:04:00 【Chongyou research Sen】
2022.6.10 Did you brush the questions today ?
subject :
Here are two arrays of integers nums1 and nums2 , Please return the intersection of two arrays as an array . Returns the number of occurrences of each element in the result , It should be consistent with the number of occurrences of elements in both arrays ( If the number of occurrences is inconsistent , Then consider taking the smaller value ). You can ignore the order of the output results .
analysis :
In the given two arrays , Find the intersection of these two arrays , The situation may be as follows :
num1【】=【1,1,2,3】 nums2【】=【1,1,1,4,5】
result :【1,1】
num1【】=【1,1,1,1,2,3】 nums2【】=【1,1,1,4,5】
result :【1,1,1】
num1【】=【1,2,3】 nums2【】=【1,1,1,2,3】
result :【1,2,3】
So the idea of this topic is , Let's start with one nums In the map Inside , And record the number of times for each value , Next we iterate over another array , And judge that these numbers are map Is there... In it , If there is , Save in vector Inside , And times -1
here -1 Operation is to ensure the first one above , Occurrence of the second case , By subtraction -1, It can ensure the minimum number of repetitions
analysis :
1. Hashtable
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. Double pointer
The idea here is , Use two pointers to point to two arrays respectively , If 1 The value of the pointer is less than 2 Pointer value , that 1 The pointer ++, conversely 2 The pointer ++, When ,1,2 When the values pointed to by the pointer are equal , Insert the value vector in , then 1,2, All hands ++
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;
}
};边栏推荐
- JS中的call()方法和apply()方法用法总结
- QT interface nested movement based on qscrollarea
- 初试c语言之第二次笔记
- Wechat applet date comparison, calculation days
- 3、再遇HandyControl之窗体
- Data Lake (VI): Hudi and Flink integration
- RSA a little bit of thought
- 【云原生 | Kubernetes篇】深入RC、RS、DaemonSet、StatefulSet(七)
- 产品开发的早期阶段,是选择开发app还是小程序?
- JMeter 中如何实现接口之间的关联?
猜你喜欢

Information theory and coding 2 final review BCH code

One-way hash function

【LogoDetection 数据集处理】(2)画出训练集图片的标注框

2022 Nanjing International Smart site equipment exhibition

Insight Technology a été sélectionné dans le rapport panorama des fournisseurs d'analyse de l'amour et d'informatique de la vie privée et a été évalué comme représentant des fournisseurs de solutions

洞见科技入选「爱分析· 隐私计算厂商全景报告」,获评金融解决方案代表厂商

How the WordPress administrator user name was leaked

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

svn外网打不开url地址怎么解决

Super practical operation! Calibration and registration of Kinect depth map and RGB camera for hands-on teaching
随机推荐
2022 the 14th Nanjing International artificial intelligence product exhibition
KaTeX问题 —— csdn编辑时中打出等号对齐的样式
[logodetection data set processing] (2) draw the label box of the training set picture
初试c语言之第二次笔记
Day10/11 recursion / backtracking
LeetCode_21(合并两个有序链表)
How to implement the association between interfaces in JMeter?
Super practical operation! Calibration and registration of Kinect depth map and RGB camera for hands-on teaching
Wechat applet returns to the previous page and transfers parameters
3. Encounter the form of handycontrol again
我的第一个Go程序
华为云SRE确定性运维介绍
一款完整的多用户微信公众平台开发源码,带文档免费分享
Data Lake (VI): Hudi and Flink integration
如何实现erp外网连接?
Notes on the second test of C language
2022 the 15th Nanjing International Digital Industry Expo
小程序网络请求Promise化
Cmake actual combat record (I)
Insight technology was selected into the "Aijian · privacy computing manufacturer panorama report" and was rated as a representative manufacturer of financial solutions