C++ 两个数组的交集

Posted qnbk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 两个数组的交集相关的知识,希望对你有一定的参考价值。

两个数组的交集


题目来源:leetcode

class Solution 
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) 

        //用unordered_set对num2中的元素去重
        unordered_set<int> s2;
        for(auto e: nums2)
        
            s2.insert(e);
        
        unordered_set<int> s1;
        for(auto e: nums1)
        
            s1.insert(e);
        
        //遍历s1,如果s1中的某个元素在s2中出现过即为交集
        vector<int> vret;
        for(auto e : s1)
        
            if(s2.find(e) != s2.end())
                
                    vret.push_back(e);
                
        
        return vret;
    
;

以上是关于C++ 两个数组的交集的主要内容,如果未能解决你的问题,请参考以下文章