Intersection of Two Arrays--求两个向量的交集

问题描述:
Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

Note:
Each element in the result must be unique.
The result can be in any order.

这里的一个难点是得出的结果会有重复,因此需要用到的工具是哈希容器
unordered_map.

使用unordered_map需要加入头文件#include

解释:无序映射表(Unordered Map)容器是一个存储以键值对组合而成的元素的关联容器(Associative container),容器中的元素无特别的次序关系。该容器允许基于主键地快速检索各个元素。

相当于一个两列矩阵,每个元素为一个值(Mapped value)绑定一个键(Key)

Example:

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

void main()
{
 unordered_map<string, int> months;
 months["january"] = 31;
 months["february"] = 28;
 months["march"] = 31;
 months["april"] = 30;
 months["may"] = 31;
 months["june"] = 30;
 months["july"] = 31;
 months["august"] = 31;
 months["september"] = 30;
 months["october"] = 31;
 months["november"] = 30;
 months["december"] = 31;
 cout << "september" << months["september"] << endl;
 cout << "april" << months["april"] << endl;
 cout << "december " << months["december"] <<endl;
 cout << "february" << months["february"] << endl;
}

回归正题,为解决上述问题可以建立一个unordered_map。第一个向量的值作为键值,对应的值为1。第二个矩阵依次遍历以它为键值的表,当对应的值大于0时,说明是两个向量中的公共元素。输出该元素后,表中值复位为0。
程序如下:

#include<iostream>
#include<iomanip>
#include<vector>
#include<unordered_map>
using namespace std;
class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> buf;
        int n1 = nums1.size();
        int n2 = nums2.size();
        vector<int> res;
        for (int i = 0;i < n1;i++)
        {
            buf[nums1[i]]++;
        }
        for (int j = 0;j < n2;j++)
        {
            if (buf[nums2[j]]>0)
            {
                res.push_back(nums2[j]);
                buf[nums2[j]]= 0;
            }
        }

        return res;
    }
};
void main()
{
    Solution s;
    vector<int> num1 = { 1,2,2,1 };
    vector<int> num2 = { 2,2 };
    vector<int> res = s.intersect(num1,num2);
    for (int i = 0;i < res.size();i++)
        cout << res[i] << endl;
}

扩展:
把交集元素全部输出,即有重复元素
Ex. Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2 2].
同样原理,只是值的表不再复位为0.程序如下:

#include<iostream>
#include<iomanip>
#include<vector>
#include<unordered_map>
using namespace std;
class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> buf;
        int n1 = nums1.size();
        int n2 = nums2.size();
        vector<int> res;
        for (int i = 0;i < n1;i++)
        {
            buf[nums1[i]]++;
        }
        for (int j = 0;j < n2;j++)
        {
            if (buf[nums2[j]]>0)
            {
                res.push_back(nums2[j]);
                buf[nums2[j]]--;//此处为区别处
            }
        }

        return res;
    }
};
void main()
{
    Solution s;
    vector<int> num1 = { 1,2,2,1 };
    vector<int> num2 = { 2,2 };
    vector<int> res = s.intersect(num1,num2);
    for (int i = 0;i < res.size();i++)
        cout << res[i] << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值