多个数组的交集

给出多个数组,求它们的交集。输出他们交集的大小。

样例

样例 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;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值