菜鸟成长逆袭之旅,爱好撸铁和撸代码,有强制的约束力,希望通过自己的努力做一个高品质人
Work together and make progress together
两个数组的交集
给定两个数组,编写一个函数来计算它们的交集。
示例 1:
输入: nums1 = [1,2,2,1], nums2 = [2,2]
输出: [2]
示例 2:
输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出: [9,4]
说明:
输出结果中的每个元素一定是唯一的。
我们可以不考虑输出结果的顺序。
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
set<int> s(nums1.begin(),nums1.end()), res;
for (auto a : nums2){
if (s.count(a)) res.insert(a);
}
return vector<int>(res.begin(),res.end());
}
};