当前位置:网站首页>[leetcode 350] intersection of two arrays II
[leetcode 350] intersection of two arrays II
2022-04-23 06:23:00 【Don't steal my energy】
Title Description
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 .
Example 1
Input :nums1 = [1,2,2,1], nums2 = [2,2]
Output :[2,2]
Example 2
Input :nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output :[4,9]
Tips
- 1 <= nums1.length, nums2.length <= 1000
- 0 <= nums1[i], nums2[i] <= 1000
Their thinking
Please pay attention to the hint , Prompt Element values in 0~1000, Using the idea of hash table , Put the elements of a container (x) As key , The number of occurrences is taken as the value . Traverse the elements in the second container (y), Element of judgement y Whether it is in the hash table , If yes, add , And the value corresponding to its key -1.
// Array implementation
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
vector<int>ans;
int a[1005]={
0};// Be sure to initialize to 0, Readers think about why ?
for(auto x:nums1)
a[x]++;
for(auto y:nums2)
if(a[y]){
ans.push_back(y);
a[y]--;
}
return ans;
}
Hash table implementation ( Array implementation will be more efficient )
vector<int> intersect1(vector<int>& nums1, vector<int>& nums2) {
vector<int>ans;
unordered_map<int,int> aa;
for(auto x:nums1)
aa[x]++;
for(auto y:nums2)
if(aa[y]){
ans.push_back(y);
--aa[y];
}
return ans;
}
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617012094.html
边栏推荐
- Customized communication between threads (reentrantlock)
- [transfer] MySQL: how many rows of data can InnoDB store in a B + tree?
- Calculation (enter the calculation formula to get the result)
- PyTorch入门小笔记——利用简单例子观察前向传播各个层输出的size
- Usage scenario of copyonwritearraylist
- Addition, deletion, modification and query of MySQL table
- Implementation of displaying database pictures to browser tables based on thymeleaf
- 20 excellent plug-ins recommended by idea
- How does MySQL convert stored seconds into dates
- Use of multithreaded executors
猜你喜欢
![Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi](/img/1b/4eea05e2634780f45b44273d2764e3.png)
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi

Integration and induction of knowledge points of automatic control principle (Han min version)

Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering

Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code

Gaussian processes of sklearn

線性代數第一章-行列式

Pyqy5 learning (2): qmainwindow + QWidget + qlabel

Advanced operation of idea debug

Filebrowser realizes private network disk

Delete and truncate
随机推荐
4. Print form
6.Reversal
Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
JDBC operation transaction
Create binary tree
自动控制原理知识点整合归纳(韩敏版)
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
9.Life, the Universe, and Everything
Graphic numpy array matrix
PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码
Common programming records - parser = argparse ArgumentParser()
Contrôle automatique (version Han min)
Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning
List segmentation best practices
8. Integer Decomposition
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
Example of ticket selling with reentrant lock
Use of multithreaded executors
Addition, deletion, modification and query of MySQL table
Linear algebra Chapter 1 - determinant