当前位置:网站首页>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());
}
};
边栏推荐
- 菜鸟看源码之ArrayList
- 智能合约dapp系统开发流程技术
- Many people don't know whether they are looking for Kanban software or Kanban software
- 349. 两个数组的交集
- logging 学习最终版-配置的不同级别日志打印的颜色
- Happens-Before原则深入解读
- @JsonFormat和@DateTimeFormat的区别和使用
- Visual conversion of nmap vulnerability scanning results
- 面试知识点
- Newbie sees the source code arraydeque
猜你喜欢

Bash shell learning notes (VII)

0x00007ffd977c04a8 (qt5sqld.dll) (in a.exe): 0xc0000005: an access violation occurred when reading position 0x0000000000000010

pytest 前后置方法

How to assemble a registry?

The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly

nmap弱点扫描结果可视化转换

Bash shell学习笔记(三)

Traversal recursion + iteration of binary tree

如何组装一个注册中心?

菜鸟看源码之ArrayList
随机推荐
软件测试综述之软件测试的背景、实质、软件开发的过程
10 let operator= return a reference to *this
面试知识点
访问权限——private,public,protected
pytest conftest.py和fixture的配合使用
很多人都不清楚自己找的是Kanban软件还是看板软件
Flutter CachedNetworkImage圆角
pytest 用例执行顺序
企鹅龙(DRBL)无盘启动+再生龙(clonezilla)网络备份与还原系统
The assignment of member pointer defined in C structure and the use of structure pointer as member function parameter
232. Implement queue with stack
Successfully transplanted stemwin v5.22 on Shenzhou IV development board
104.二叉树的最大深度
Sword finger offer (53): a string representing a numeric value
Visual conversion of nmap vulnerability scanning results
C language pengge 20210812c language function
字典与int矩阵
PLC与伺服电机连接
Bash shell学习笔记(七)
Bash shell learning notes (V)