• 721. Accounts Merge


    https://leetcode.com/problems/accounts-merge/description/

    class UnionFound {
    public:
        unordered_map<int,int> parents;
        int cnt = 0;
        void AddItem(int i) {
            parents[i] = i;
            cnt++;
        }
        int GetParent(int i) {
            if (parents[i] == i)    return i;
            return parents[i] = GetParent(parents[i]);
        }
        void Union(int p, int c) {
            int pp = GetParent(p);
            int cp = GetParent(c);
            if (pp != cp) {
                parents[cp] = pp;
                cnt--;
            }
        }
    };
    class Solution {
    public:
        vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
            UnionFound uf;
            for (int i = 0; i < accounts.size(); i++)
                uf.AddItem(i);
            
            unordered_map<string, int> map_email_to_person;
            for (int i = 0; i < accounts.size(); i++)
                for (int j = 1; j < accounts[i].size(); j++) {
                    const auto& email = accounts[i][j];
                    if (map_email_to_person.count(email)) {
                        uf.Union(map_email_to_person[email], i);
                    }
                    else {
                        map_email_to_person[email] = i;
                    }
                }
            
            unordered_map<int, set<string>> map_person_to_emails;
            for (int i = 0; i < accounts.size(); i++) {
                int person = uf.GetParent(i);
                for (int j = 1; j < accounts[i].size(); j++) {
                    const auto& email = accounts[i][j];
                    map_person_to_emails[person].insert(email);
                }
            }
            
            vector<vector<string>> res;
            for (const auto& it : map_person_to_emails) {
                vector<string> p;
                p.push_back(accounts[it.first][0]);
                for (const auto & email : it.second)
                    p.push_back(email);
                res.push_back(p);
            }
            return res;
        }
    };
  • 相关阅读:
    Linux配置java环境
    三级联动的实现
    Linux安装
    省市区县的sql语句——城市
    shiro登陆权限验证
    省市区县的sql语句——区县
    Linux安装Jenkins
    省市区县的sql语句——省
    读《世界是数字的》有感
    读《我是一只IT小小鸟》有感
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/8994768.html
Copyright © 2020-2023  润新知