思路:用两个map分别存储两个列表内容,然后再对照即可
class Solution { public: vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) { unordered_map<int, int> mp1; unordered_map<int, int> mp2; int ans1 = 0, ans2 = 0; for (int i : nums1) { mp1[i]++; } for (int j : nums2) { mp2[j]++; } for (int i : nums1) { if (mp2[i]) { ans1++; } } for (int i : nums2) { if (mp1[i]) { ans2++; } } vector<int> res; res.push_back(ans1); res.push_back(ans2); return res; } };
优化:初始化用集合set并且直接放入即可
标签:int,res,ans2,ans1,++,vector,数组,公共,2956 From: https://www.cnblogs.com/mysteryily/p/18306307