题解 | 两个数组的交集
两个数组的交集
https://www.nowcoder.com/practice/f31371f27dcd4c90a8fd2902d3e4592c
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums1 int整型vector
* @param nums2 int整型vector
* @return int整型vector
*/
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int,bool>mp;
vector<int>rs;
for (int i:nums1)
{
if (mp.find(i) == mp.end())
mp[i] = true;
}
for (int i:nums2)
{
if (mp[i])
{
mp[i] = false;
rs.push_back(i);
}
}
sort(rs.begin(),rs.end());
return rs;
}
};


