题目
给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails 表示该帐户的邮箱地址。
现在,我们想合并这些帐户。如果两个帐户都有一些共同的邮件地址,则两个帐户必定属于同一个人。请注意,即使两个帐户具有相同的名称,它们也可能属于不同的人,因为人们可能具有相同的名称。一个人最初可以拥有任意数量的帐户,但其所有帐户都具有相同的名称。
合并帐户后,按以下格式返回帐户:每个帐户的第一个元素是名称,其余元素是按顺序排列的邮箱地址。accounts 本身可以以任意顺序返回。
例子 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:
第一个和第三个 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']]仍然会被接受。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/accounts-merge
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
该题使用并查集求解,使用并查集需要先明确需要将什么作为并查集的每个元素?(这里有n个账户,每个账户有不同个数的邮箱)。答案是将n个账户作为并查集的每个元素,根据每个账户之间的父子关系可以判断账户间是否为同一个人拥有的,用一个哈希表emails
存储已经出现过的邮箱,key为邮箱,value为该邮箱所属的账户的下标(用户的名字可能重复)。在遍历每个账户的邮箱时首先判断该邮箱是否已经出现过,若出现则表明该账户和前面的其中一个账户为同一个账户,找到该邮箱对应的下标并将当前账户下标和之前的下标合并(union)。遍历完所有邮箱后就已经处理好各个账户之间的父子关系。
使用一个哈希表resMap
存储结果,key为每个账户的下标(账户名会重复不能作为key),value为该账户的所有邮箱(Set,去除重复的邮箱),遍历0~n,利用并查集找到(find(i)
)当前下标的父亲f
,将该账户的所有邮箱添加到父亲的邮箱中。最后将账户下标变为账户的名字即可得到结果。
实现需要对java的哈希表、set、List等熟练操作。
class UnionFindSet {
public int[] parent;
public UnionFindSet(int n) {
parent = new int[n];
for (int i = 0; i < parent.length; i++)
parent[i] = -1;
}
public int find(int x) {
if (parent[x] < 0) {
return x;
} else {
// path compression.
parent[x] = find(parent[x]);
}
return parent[x];
}
public void union(int x, int y) {
int xx = find(x);
int yy = find(y);
if (xx != yy) {
parent[yy] = xx;
}
}
}
class Solution {
public List<List<String>> accountsMerge(List<List<String>> accounts) {
int n = accounts.size();
Map<String, Integer> emailsMap = new HashMap();
UnionFindSet ufset = new UnionFindSet(n);
// travel all the emails and get ufset.
for (int i = 0; i < n; i++) {
List<String> emails = accounts.get(i);
String name = emails.get(0);
for (int j = 1; j < emails.size(); j++) {
String email = emails.get(j);
if (emailsMap.containsKey(email)) {
// this email has already appeared.
int id = emailsMap.get(email);
// union this two accounts.
ufset.union(id, i);
} else
emailsMap.put(email, i);
}
}
// according to the ufset, we can know which accounts belong to the same person.
Map<Integer, Set<String>> resMap = new HashMap<>();
for (int i = 0; i < n; i++) {
// i's emails.
List<String> iemails = accounts.get(i).subList(1, accounts.get(i).size());
int f = ufset.find(i);
// put all i's emails to f
if (resMap.containsKey(f))
resMap.get(f).addAll(iemails);
else
resMap.put(f, new TreeSet<>(iemails));
}
List<List<String>> res = new LinkedList<>();
for (Integer key : resMap.keySet()) {
List<String> account = new LinkedList<>();
account.add(accounts.get(key).get(0)); // name
account.addAll(resMap.get(key));
res.add(account);
}
return res;
}
}