349 Intersection of Two Arrays

Given two arrays, write a function to compute their intersection.
Example:
 Given num1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
Hint:
    Each element in the result must be unique.
    We can ignore the order of output results.
See: https://leetcode.com/problems/intersection-of-two-arrays/description/
C++:

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        set<int> s(nums1.begin(),nums1.end()),res;
        for(auto num:nums2)
        {
            if(s.count(num))
            {
                res.insert(num);
            }
        }
        return vector<int>(res.begin(),res.end());
    }
};

 Reference: https://www.cnblogs.com/grandyang/p/5507129.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324382555&siteId=291194637