题目
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; } };
- step1: 先扫描一遍数组,建立邮箱与邮箱所在