中等
给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails 表示该账户的邮箱地址。
现在,我们想合并这些账户。如果两个账户都有一些共同的邮箱地址,则两个账户必定属于同一个人。请注意,即使两个账户具有相同的名称,它们也可能属于不同的人,因为人们可能具有相同的名称。一个人最初可以拥有任意数量的账户,但其所有账户都具有相同的名称。
合并账户后,按以下格式返回账户:每个账户的第一个元素是名称,其余元素是按顺序排列的邮箱地址。账户本身可以以任意顺序返回。
示例 1:
输入:
accounts = [[“John”, “johnsmith@mail.com”, “john00@mail.com”], [“John”, “johnnybravo@mail.com”], [“John”, “johnsmith@mail.com”, “john_newyork@mail.com”], [“Mary”, “mary@mail.com”]]
输出:
[[“John”, ‘john00@mail.com’, ‘john_newyork@mail.com’, ‘johnsmith@mail.com’], [“John”, “johnnybravo@mail.com”], [“Mary”, “mary@mail.com”]]
解释:
第一个和第三个 John 是同一个人,因为他们有共同的邮箱地址 “johnsmith@mail.com”。
第二个 John 和 Mary 是不同的人,因为他们的邮箱地址没有被其他帐户使用。
可以以任何顺序返回这些列表,例如答案 [[‘Mary’,‘mary@mail.com’],[‘John’,‘johnnybravo@mail.com’],
[‘John’,‘john00@mail.com’,‘john_newyork@mail.com’,‘johnsmith@mail.com’]] 也是正确的。
提示:
accounts的长度将在[1,1000]的范围内。
accounts[i]的长度将在[1,10]的范围内。
accounts[i][j]的长度将在[1,30]的范围内。
分析:
关键是学习 本题中哈希表的用法和并查集的用法,非常巧妙~
根据题意,相同的邮箱名肯定是属于同一个人的。在这里,都是列表,不好进行操作,我们可以想到的是 使用 2个哈希表,1个将每一个邮箱映射到不同的name,另一个将每一个邮箱映射到的不同的编号,进行按照编号进行合并,合并我们使用的是并查集,所以需要邮箱映射到编号的哈希表。
在上面一步完成以后。
我们遍历每一个account,我们对同一个account中的email进行合并,因为同一个account中的邮箱肯定是属于同一个人的~,合并完成以后,我们的并查集中的集合 就是对应一个account,此时已经合并完成。
这时我们需要将每个集合输出出来,这里输出的方法 也值得学习。
先创建一个indexToEmails哈希表, integer ->List string类型
我们遍历 emailToIndex.keySet()集合,对于其中的每一个key ,就是每一个email,找到它属于这个编号的集合,然后将这个email添加到对应的集合中去,然后将这个集合 add进 indextoEmails这个集合中
最后需要将name添加到每个集合的最前面,同时对集合内的邮箱进行排序,进行输出即可!
代码如下:
class Solution {
public List<List<String>> accountsMerge(List<List<String>> accounts) {
Map<String,Integer> emailToIndex = new HashMap<>();
Map<String,String> emailToName = new HashMap<>();
int emailCount = 0;
//把每个邮箱 对应的编号和 主人名字 放入哈希表中
for(List<String> account:accounts){
String name = account.get(0);
int size = account.size();
for(int i = 1;i < size;i++){
String email = account.get(i);
if(!emailToIndex.containsKey(email)){
emailToIndex.put(email,emailCount++);
emailToName.put(email,name);
}
}
}
UnionFind uf = new UnionFind(emailCount);
//现在有emailcount个不同的邮箱,对于每个account,其中的邮箱肯定都是属于同一个人的
for(List<String> account : accounts){
String firstEmail = account.get(1);
int firstIndex = emailToIndex.get(firstEmail);
int size = account.size();
for(int i = 2;i < size;i++){
String nextemail = account.get(i);
int nextIndex = emailToIndex.get(nextemail);
//将同一个人的邮箱 按照编号进行合并,集合合并
uf.union(firstIndex,nextIndex);
}
}
//每一个集合中 所对应的邮箱个数,
//这里的哈希表的用法 也是值得学习的!!!
Map<Integer,List<String>> indexToEmails = new HashMap<Integer,List<String>>();
for(String eamil : emailToName.keySet()){
//找出找个邮箱的编号,即属于哪个集合!!!
int index = uf.find(emailToIndex.get(eamil));
//查看该编号对应的集合 是不是已经存在,如果已经存在,则取出来,加入一个新的amail
//如果该编号所对应的集合中还没有邮箱 则新创建一个列表
List<String> account = indexToEmails.getOrDefault(index,new ArrayList<String>());
account.add(eamil);
indexToEmails.put(index,account);
}
//现在 一个集合中的邮箱全部都是属于同一个人的了,但是还不是按照题目要求的格式 输出
List<List<String>> merged = new ArrayList<List<String>>();
for(List<String> emails : indexToEmails.values()){
//内置API 进行排序
Collections.sort(emails);
String name = emailToName.get(emails.get(0));
List<String> account = new ArrayList<String>();
account.add(name);
account.addAll(emails);
merged.add(account);
}
return merged;
}
private class UnionFind{
private int[] parent;
private UnionFind(int n){
parent = new int[n];
for(int i = 0;i < n;i++){
parent[i] = i;
}
}
public int find(int x){
if(x != parent[x]){
parent[x] = find(parent[x]);
}
return parent[x];
}
public void union(int x,int y){
int rootx = find(x);
int rooty = find(y);
if(rootx == rooty){
return;
}
parent[rootx] = rooty;
}
}
}