首页 > 编程语言 > 详细

349 Intersection of Two Arrays 两个数组的交集

时间:2018-04-15 11:19:45      阅读:203      评论:0      收藏:0      [点我收藏+]

给定两个数组,写一个函数来计算它们的交集。
例子:
 给定 num1= [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].
提示:
    每个在结果中的元素必定是唯一的。
    我们可以不考虑输出结果的顺序。
详见: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());
    }
};

 参考:https://www.cnblogs.com/grandyang/p/5507129.html

349 Intersection of Two Arrays 两个数组的交集

原文:https://www.cnblogs.com/xidian2014/p/8836549.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!