721. Accounts Merge

题目

Given a list accounts, each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account.

Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some email that is common to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.

After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.

Example 1:

Input: 
accounts = [["John", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]]
Output: [["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'],  ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]]
Explanation: 
The first and third John's are the same person as they have the common email "johnsmith@mail.com".
The second John and Mary are different people as none of their email addresses are used by other accounts.
We could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'], 
['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.

Note:

  • The length of accounts will be in the range [1, 1000].
  • The length of accounts[i] will be in the range [1, 10].
  • The length of accounts[i][j] will be in the range [1, 30].
  • 思路

    本题又是个典型的连通性问题,思路简单容易理解,但是容易出错,主要分以下几步:

    • step1: 先扫描一遍数组,建立邮箱与邮箱所在vector<vector<string>>数组中的id号
    • step2:第二遍确定连通分量并做连接存储
    • step3: 接下来遍历id数组,寻找根节点
    • step4:接下来遍历hashmap,合并连通项

    Note:这里的有序可以通过set进行默认排序

    代码

    class Solution {
    public:
        vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
            //先扫描一遍数组,建立邮箱与邮箱所在vector<vector<string>>数组中的id号
            unordered_map<string,vector<int>> mp;
            for(int i=0;i<accounts.size();i++)
            {
                for(int j=1;j<accounts[i].size();j++)
                {
                    mp[accounts[i][j]].push_back(i);
                }
            }
            
            //第二遍确定连通分量并做连接存储
            unordered_map<string,vector<int>>::iterator itr = mp.begin();
            vector<int> id;
            for(int i=0;i<accounts.size();i++)
                id.push_back(i);
           //cout<<1<<endl;
            while(itr!=mp.end())
            {
                if(itr->second.size()<=1)
                {
                    itr++;
                    continue;
                }
                else
                {
                    for(int i=0;i<itr->second.size()-1;i++)
                    {
                        int p = itr->second[i];
                        int q = itr->second[i+1];
                        while(p!=id[p])
                            p = id[p];
                        while(q!=id[q])
                            q = id[q];
                        if(p!=q)
                            id[p] = q;
                    }
                }
                itr++;
            }
            //cout<<2<<endl;
            //接下来遍历id数组,寻找根节点
            unordered_map<int ,vector<int>> res;
            for(int i=0;i<accounts.size();i++)
            {
                int root = i;
                while(root!=id[root])
                    root = id[root];
                res[root].push_back(i); 
            }
           
            //接下来遍历hashmap,合并连通项
            vector<vector<string>> result;
            unordered_map<int ,vector<int>>::iterator it = res.begin();
            while(it!=res.end())
            {
                vector<string> mail;
                string name;
               // cout<<res->first<<endl;
                for(int i=0;i<it->second.size();i++)
                {
                    name = accounts[it->second[i]][0];
                    mail.insert(mail.end(),accounts[it->second[i]].begin()+1,accounts[it->second[i]].end());
                }
                set<string> s(mail.begin(),mail.end());
                vector<string> tm(s.begin(),s.end());
                tm.insert(tm.begin(),name);
                result.push_back(tm);
                it++;
            }
            return result;
        }
    };
    
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小虎AI实验室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值