LintCode-Intersection of Arrays

給出多個數組,求它們的交集。輸出他們交集的大小。

 注意事項
輸入的所有數組元素總數不超過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();
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章