给出多个数组,求它们的交集。输出他们交集的大小。
注意事项
输入的所有数组元素总数不超过500000。
题目数据每个数组里的元素没有重复。题目不写
数据规模,还以为是什么没见过的算法,结果用set查找就行。
不用stl自己写的话估计要做个二叉搜索树什么的。
class Solution {
public:
/**
* @param arrs: the arrays
* @return: the number of the intersection of the arrays
*/
int intersectionOfArrays(vector<vector<int>> &arrs) {
// write your code here
set<int> a;
if(arrs.size()==0) return 0;
for(auto& var:arrs[0])
{
a.insert(var);
}
for(auto& array:arrs)
{
set<int> b;
for(auto& var:array)
{
if(a.find(var)!=a.end())
{
b.insert(var);
}
}
a=move(b);
}
return a.size();
}
};