当前位置:网站首页>349. Intersection of two arrays
349. Intersection of two arrays
2022-07-26 10:54:00 【Forest_ one thousand and ten】

First, use two sets to store the elements in two arrays , Then traverse one of the sets , Determine whether each element is in another set , If the element is also in another collection , The element is added to the return value . The time complexity of this method can be reduced to O(m+n)
Sets are divided into three types :
- std::set
Underlying implementation : Red and black trees
Is it orderly : Orderly
Whether the value can be repeated : no
The query efficiency :O(logn) - std::multiset
Underlying implementation : Red and black trees
Is it orderly : Orderly
Whether the value can be repeated : yes
The query efficiency :O(logn) - std::unordered_set
Underlying implementation : Hashtable
Is it orderly : disorder
Whether the value can be repeated : no
The query efficiency :O(1)
Sum up , Use unordered_set Efficiency is the highest .
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> result_set;
unordered_set<int> nums1_set(nums1.begin(), nums1.end());// take num1 The data in is stored in nums1_set
unordered_set<int> nums2_set(nums2.begin(), nums2.end());//nums1_set,nums2_set There are no repeated elements in
for (int num : nums2_set) // Traverse nums2_set: From an array nums2 Take out the elements in turn and assign them to integer variables nums, Loop execution for Middle sentence
{
// If There is : lookup num Whether there is , There is an iterator that returns this element , There is no return nums1_set.end();
if (nums1_set.find(num) != nums1_set.end()) // In collection 1 Search for , There are also collections found 2 This element in
{
result_set.insert(num);
}
}
return vector<int>(result_set.begin(), result_set.end());
}
};
边栏推荐
- Bash shell learning notes (I)
- 微信公众号消息通知 “errcode“:40164,“errmsg“:“invalid ip
- RT thread learning notes (I) -- configure RT thread development environment
- Tutorial of putty
- Flutter jni混淆 引入.so文件release包闪退
- nmap弱点扫描结果可视化转换
- pytest fixture装饰器
- Sword finger offer (52): regularization expression
- C#halcon用户控件崩溃的一种处理方法
- Sword finger offer (53): a string representing a numeric value
猜你喜欢

Successfully transplanted stemwin v5.22 on Shenzhou IV development board

RT thread learning notes (III) -- building a compilation environment with scons

Wireshark basic tutorial Ethernet frame analysis.

Linkedblockingqueue of novice source code

ThreadPoolExecutor是怎样执行任务的

Bash shell learning notes (VII)

菜鸟看源码之HashTable

菜鸟看源码之ArrayList

flutter 背景变灰效果,如何透明度,灰色蒙板遮罩

微信公众号消息通知 “errcode“:40164,“errmsg“:“invalid ip
随机推荐
MFC中0x003b66c3 处有未经处理的异常: 0xC000041D: 用户回调期间遇到未经处理的异常
Flutter jni混淆 引入.so文件release包闪退
13 managing resources by objects
-bash: ./build.sh: /bin/bash^M: 坏的解释器: 没有那个文件或目录
菜鸟看源码之SparseArray
Flutter TextField怎样去除下划线及有焦点时颜色
微信公众号消息通知 “errcode“:40164,“errmsg“:“invalid ip
How to assemble a registry?
232.用栈实现队列
回到顶部的几种方案(js)
Successfully transplanted stemwin v5.22 on Shenzhou IV development board
349. 两个数组的交集
104.二叉树的最大深度
mysql20210906
Sword finger offer (twenty): stack containing min function
C#halcon用户控件崩溃的一种处理方法
Sword finger offer (V): queue with two stacks
What are the biz layer and manager layer in the company project
0x00007FFD977C04A8 (Qt5Sqld.dll)处(位于 a.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000010 时发生访问冲突
1748.唯一元素的和