当前位置:网站首页>Force buckle 349 Intersection of two arrays
Force buckle 349 Intersection of two arrays
2022-06-30 04:50:00 【A wise man should not be bald】
Given two arrays nums1
and nums2
, return Their intersection . Every element in the output must be only Of . We can Regardless of the order of the output results .
Special attention required : Each element of the output must be unique , That is, the output is de duplicated , At the same time, the order does not need to be considered
Using arrays to do hashing , Because the problem limits the size of the value , This question does not limit the number of occurrences of each element , So you don't have to use arrays .
And if the hash is small , Very scattered 、 The span is very large , Using arrays can be a huge waste of space .
Now it's time to use another structure ,set , About set,C++ We provide the following three kinds of data structures :
- std::set
- std::multiset
- std::unordered_set
std::set and std::multiset The underlying implementations are all red black trees ,std::unordered_set The underlying implementation of is a hash table , Use unordered_set Reading and writing efficiency is the highest , There's no need to sort the data , And don't duplicate the data , So choose unordered_set.
The idea is shown in the picture :
Method :unordered_set
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
// Store the returned results
unordered_set<int> result_set;
unordered_set<int> nums_set(nums1.begin(), nums1.end());
for(int num : nums2) {
// Find out nums2 The elements in nums1 in , Record the element in the returned result set
if(nums_set.find(num) != nums_set.end()) {
result_set.insert(num);
}
}
// Note that what you need to return here is an array
return vector<int>(result_set.begin(), result_set.end());
}
};
边栏推荐
- Check London attractions suitable for parents and children in winter vacation
- UE4 method of embedding web pages
- SSL update method
- Geotrustov wildcard
- A collection of errors encountered in machine learning with unity
- JPA复合主键使用
- harbor api 2.0查询
- 2021-03-16
- How to renew an SSL certificate
- How to repair expired SSL certificates?
猜你喜欢
Royal Albert Hall, a popular landmark in London
Interprocess communication
Deeply understand the function calling process of C language
Malignant bug: 1252 of unit MySQL export
图的一些表示方式、邻居和度的介绍
【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator
Connect to the database and run node JS running database shows that the database is missing
EasyRecovery数据恢复软件 恢复了我两年前的照片视频数据
【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai
Free travel recommendation in Bangkok: introduction to the Mekong River in Bangkok
随机推荐
The difference between get and post requests
Unity lens making
【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai
One interview question and one joint index every day
[FPGA] IIC读写EEPROM 的实现
Network high concurrency
[control] multi agent system summary. 5. system consolidation.
Paging query, using jdbc-- paging query
史上最全的Redis基础+进阶项目实战总结笔记
Foreign SSL certificate
Difference between TCP three handshakes and four waves and tcp/udp
Redis implements SMS login function (I) traditional session login
Efficiency test of adding and querying ArrayList and LinkedList
Process architecture and process management
Deeply understand the function calling process of C language
Cheap SSL certificate abroad
This connection is not a private connection this website may be pretending to steal your personal or financial information
Approaching history, introduction to the London Guard Museum
Unity enables simple music visualization
【Paper】2021_ Observer-Based Controllers for Incrementally Quadratic Nonlinear Systems With Disturbanc