欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

两个数组的交集2

程序员文章站 2022-07-15 22:34:22
...

两个数组的交集2

class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> s1;
		for (auto e : nums1)
		{
			s1[e]++;
		}
		unordered_map<int, int> s2;
		for (auto e : nums2)
		{
			s2[e]++;
		}
		vector<int> v;
		for (auto e : s1)
		{
            auto s2tmp = s2.find(e.first);
			if(s2tmp != s2.end())
            {
                int tmp = s2tmp->second < e.second ? s2tmp->second : e.second;
                while(tmp--)
                {
                    v.push_back(e.first);
                }
            }
		}
		return v;
    }
};