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].

 

解题思路:

这道题目可以用Union Find 来解决。参考了GrandYang的解法

ret: 存储整理好后的邮箱

root: 存储父亲节点(将每个的第一个邮箱作为父亲节点) 

owner: 存储每个节点对应的人

m: 为了防止重复以及对邮箱进行直接排序,使用set

 

将每个邮箱作为一个节点

1. 开始时初始化每个节点的父节点为其自己,并且记录相关的主人.

2. 将每一个邮箱的父节点设置成为头部的节点

3. 将每一个邮箱加入到对应的集合中

4. 从集合中取出点(此时已为有序的),在头部插入主人的名字,压入返回数组

 

代码:

class Solution {
public:
    vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
        vector<vector<string>> ret;
        unordered_map<string, string> root;
        unordered_map<string, string> owner;
        unordered_map<string, set<string>> m;
        
        for(auto acc : accounts){
            for(int i = 1; i < acc.size(); i++ ){
                root[acc[i]] = acc[i];
                owner[acc[i]] = acc[0];
            }
        }
        
        for(auto acc : accounts){
            string p = find(acc[1], root);
            for(int i = 2; i < acc.size(); i++){
                root[find(acc[i], root)] = p;
            }
        }
        
        for(auto acc : accounts){
            for(int i = 1; i < acc.size(); i++){
                m[find(acc[i], root)].insert(acc[i]);
            }
        }
        for(auto a : m){
            vector<string> v(a.second.begin(), a.second.end());
            v.insert(v.begin(), owner[a.first]);
            ret.push_back(v);
        }
        return ret;
    }
    string find(string s, unordered_map<string, string> &root) {
        if(root[s] == s)
            return s;
        return root[s] =find(root[s], root);
    }
};

 

posted @ 2018-07-11 09:23  妖域大都督  阅读(112)  评论(0编辑  收藏  举报