当前位置:网站首页>[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
边栏推荐
- MySQL best practices for creating tables
- 线性代数第二章-矩阵及其运算
- Three ways to create threads
- Algèbre linéaire chapitre 2 - matrice et son fonctionnement
- Algèbre linéaire chapitre 1 - déterminants
- PHP processing JSON_ Decode() parses JSON stringify
- Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
- Exception handling: grab and throw model
- MySQL advanced query
- Kibana search syntax
猜你喜欢

线代第四章-向量组的线性相关
![Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention](/img/4e/1a51636853d11544e6f5c37a588730.png)
Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention

Pyqy5 learning (2): qmainwindow + QWidget + qlabel

Illustrate the significance of hashcode
![图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi](/img/1b/4eea05e2634780f45b44273d2764e3.png)
图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi

Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()

Techniques et principes de détection

Understanding and installing MySQL

Kibana search syntax

Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
随机推荐
JDBC connection database
Usage scenario of copyonwritearraylist
Custom exception class
2. Devops sonar installation
Three ways to create threads
Example of reentrant lock thread waiting to wake up
Numpy common function table sorting of data processing
Failure to deliver XID in Seata distributed transaction project
Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention
常用编程记录——parser = argparse.ArgumentParser()
PHP processing JSON_ Decode() parses JSON stringify
Collections multiple parameter sorting
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
How to grow at work
LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
Contrôle automatique (version Han min)
MySQL basic madness theory
Understanding and use of tp50, tp90 and tp99
Create binary tree
Kibana search syntax