题目描述
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]
.
题意:给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails 表示该帐户的邮箱地址。现在,我们想合并这些帐户。如果两个帐户都有一些共同的邮件地址,则两个帐户必定属于同一个人。请注意,即使两个帐户具有相同的名称,它们也可能属于不同的人,因为人们可能具有相同的名称。一个人最初可以拥有任意数量的帐户,但其所有帐户都具有相同的名称。合并帐户后,按以下格式返回帐户:每个帐户的第一个元素是名称,其余元素是按顺序排列的邮箱地址。accounts 本身可以以任意顺序返回。
算法1
(并查集)
题解:因为有相同邮箱的账户我们认为是同一个人,因此我们可以使用并查集来维护这种关系。我们使用一个unordered_map<string,int> hash_root
来维护邮箱的拥有者是谁,首先遍历所有的邮箱,如果当前邮箱没有出现过,即插入哈希表,如果出现过了,就将哈希表中的拥有者与当前帐号的拥有者合并。最后将每一个合并块中的所有用户的所有邮箱用一个set
过滤。因为set
默认就是字典序的,可以很好的完成我们的目标。
C++ 代码
class Solution {
public:
vector<int> fa,size;
unordered_map<string,int> hash_root;
int n,t = 0;
int getfather(int x)
{
if(x != fa[x]) fa[x] = getfather(fa[x]);
return fa[x];
}
void uni(int x,int y)
{
int fx = getfather(x),fy = getfather(y);
if(fx != fy)
{
if(size[fx] < size[fy])
{
fa[fx] = fy;
size[fy] += size[fx];
}else
{
fa[fy] = fx;
size[fx] += size[fy];
}
}
}
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
n = accounts.size();
fa = vector<int>(n,0),size = vector<int>(n,1);
for(int i = 0 ; i < n ; i ++) fa[i] = i;
for(int i = 0 ; i < n ; i ++)
{
for(int j = accounts[i].size() - 1; j > 0 ; j --)
{
if(hash_root.find(accounts[i][j]) == hash_root.end())
hash_root[accounts[i][j]] = i;
else
uni(i,hash_root[accounts[i][j]]);
}
}
unordered_map<int,set<string>> hash;
for(int i = 0 ; i < n ; i ++)
{
fa[i] = getfather(i);
if(fa[i] == i) t ++;
for(int j = accounts[i].size() - 1 ; j > 0 ; j --)
hash[fa[i]].insert(accounts[i][j]);
}
vector<vector<string>> res(t);
for(auto it:hash)
{
t --;
vector<string> cur ;
cur.push_back(accounts[it.first][0]);
for(auto email:it.second)
cur.push_back(email);
res[t] = cur;
}
return res;
}
};
附个缩减版
请问hash_root.find(accounts[i][j]) == hash_root.end()中这个hash_root.end()是啥作用?和最后一个相等么
在哈希表中查找这个
key
存在不,存在会返回指向它
的迭代器,不存在的话返回指向末尾的下一个
迭代器,也就是..end()
懂了,谢谢