349.两个数组定交集

题目:[https://leetcode-cn.com/problems/intersection-of-two-arrays/description/]
思路:统计每个数定出现次数,输出出现两次
代码:

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        set<int> a(nums1.begin(),nums1.end());
        set<int> b(nums2.begin(),nums2.end());
        map<int,int> temp;
        vector<int> ans;
        for (int x:a) {
            ++temp[x];
        }
        for (int y:b) {
            ++temp[y];
        }
        for (auto v:temp) {
            if (v.second>1) {
                ans.push_back(v.first);
            }
        }
        return ans;
    }
};
posted @ 2018-09-19 15:30  Kipper  阅读(136)  评论(0编辑  收藏  举报