给出多个数组,求它们的交集。输出他们交集的大小。
样例
样例 1:
输入: [[1,2,3],[3,4,5],[3,9,10]]
输出: 1
解释:
只有3出现在三个数组中。
样例 2:
输入: [[1,2,3,4],[1,2,5,6,7][9,10,1,5,2,3]]
输出: 2
解释:
交集是[1,2].
注意事项
- The total number of all array elements is not more than
500000
. - There are no duplicated elements in each array.
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
int ret = 0;
set<int> myset[arrs.size()];
for(int i = 0; i < arrs.size(); i++)
{
//myset[i] = set(arrs[i].begin(), arrs[i].end());
for(int j = 0; j < arrs[i].size(); j++)
{
myset[i].insert(arrs[i][j]);
}
}
for(int i = 0; i < arrs[0].size(); i++)
{
bool tmp = true;
for(int j = 1; j < arrs.size(); j++)
{
if((myset[j].find(arrs[0][i])) == myset[j].end())
{
tmp = false;
break;
}
}
if(tmp == true)
{
ret++;
cout<<arrs[0][i]<<endl;
}
}
return ret;
}
};