Description

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

 
Example 1:
Input:
[
["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. You 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']
]
is also acceptable.

 解题思路: 使用并查集。1. 建立 Email -> List[User] 关系。 2. 将属于同一Email的user 使用并查集连接。 3. 建立User -> List[Email] 4. 根据并查集 找到每个User的老大哥,以老大哥为代表输出旗下所以Email。

 public class Solution {
/**
* @param accounts: List[List[str]]
* @return: return a List[List[str]]
*/
int[] father;
public List<List<String>> accountsMerge(List<List<String>> accounts) {
father = new int[accounts.size()];
for (int i = 0; i < accounts.size(); i++) {
father[i] = i;
} //union
Map<String, List<Integer>> emailToIds = getEmailToIds(accounts);
for (List<Integer> ids: emailToIds.values()) {
for (int i = 1; i < ids.size(); i++) {
union(ids.get(i), ids.get(0));
}
} //merge
Map<Integer, Set<String>> idToEmailSet = getIdToEmailSet(accounts);
List<List<String>> mergedAccounts = new ArrayList<>();
for (Map.Entry<Integer, Set<String>> entry : idToEmailSet.entrySet()) {
Integer userId = entry.getKey();
List<String> emails = new ArrayList(entry.getValue());
Collections.sort(emails);
emails.add(0, accounts.get(userId).get(0));
mergedAccounts.add(emails);
}
return mergedAccounts;
} private Map<String, List<Integer>> getEmailToIds(List<List<String>> accounts) {
Map<String, List<Integer>> emailToIds = new HashMap<>();
for (int userId = 0; userId < accounts.size(); userId++) {
List<String> account = accounts.get(userId);
for (int i = 1; i < account.size(); i++) {
List<Integer> ids = emailToIds.getOrDefault(account.get(i), new ArrayList<Integer>());
ids.add(userId);
emailToIds.put(account.get(i), ids);
}
}
return emailToIds;
} private Map<Integer, Set<String>> getIdToEmailSet(List<List<String>> accounts) {
Map<Integer, Set<String>> idToEmailSet = new HashMap<>();
for (int userId = 0; userId < accounts.size(); userId++) {
List<String> account = accounts.get(userId);
int root = find(userId);
Set<String> emails = idToEmailSet.getOrDefault(root, new HashSet<String>());
for (int i = 1; i < account.size(); i++) {
emails.add(account.get(i));
}
idToEmailSet.put(root, emails);
}
return idToEmailSet;
} private void union (int id1, int id2) {
int root1 = find(id1);
int root2 = find(id2);
if (root1 != root2) {
father[root1] = root2;
}
} private int find(int id) {
int node = id;
while (father[node] != node) {
node = father[node];
}
int root = node;
while (id != root) {
int tmp = father[id];
father[id] = root;
id = tmp;
}
return root;
} }

Accounts Merge的更多相关文章

  1. 【LeetCode】721. Accounts Merge 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/accounts ...

  2. [LeetCode] Accounts Merge 账户合并

    Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...

  3. [Swift]LeetCode721. 账户合并 | Accounts Merge

    Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...

  4. [leetcode]721. Accounts Merge账户合并

    Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...

  5. 721. Accounts Merge合并电子邮件账户

    [抄题]: Given a list accounts, each element accounts[i] is a list of strings, where the first element  ...

  6. [LeetCode] 721. Accounts Merge 账户合并

    Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...

  7. LeetCode 721. Accounts Merge

    原题链接在这里:https://leetcode.com/problems/accounts-merge/ 题目: Given a list accounts, each element accoun ...

  8. 【leetcode】721. Accounts Merge(账户合并)

    Given a list of accounts where each element accounts[i] is a list of strings, where the first elemen ...

  9. 721. Accounts Merge

    https://leetcode.com/problems/accounts-merge/description/ class UnionFound { public: unordered_map&l ...

随机推荐

  1. Pytorch 网络结构可视化

    安装 conda install graphvizconda install tensorwatch 载入库 import sysimport torchimport tensorwatch as t ...

  2. .NET Core IOC AOP

    IOC简介 IOC思想 把类当做组件或服务来看待,组件内一定要高内聚,组件之间一定要低耦合,既然要保持低耦合,那就一定不要轻易的去new什么对象. 那组件之间的交互怎么处理呢?那最好的方式就是把new ...

  3. JDK1.8 的 HashMap 源码之注意事项

    文章目录 链表变树 树形结构与Comparable,性能极致与降低 链表与树之间转换的阈值 英语渣靠着翻译插件,大概翻译的,难免有错误之处,注意甄别: 链表变树 This map usually ac ...

  4. Word 固定行间距公式图片显示不全、Word Eculid 字体导致行间距过大、Word 行间距过大

    1. 前言 1.有些文章行间距要求是固定值,比如,固定值15磅,但是这样会导致有些公式.图片显示不全.例如下图: 2.Euclid这个字体很容易导致行间距超大. 2. 解决方案 1.把固定值15磅改为 ...

  5. C之typedef

    1.1 typedef 解析: 1.typedef是一个关键字: 2.typedef它的对象必须是一个类型: 3.作用:给类型取一个别名 1.2 typedef 格式 : typedef  类型    ...

  6. WUSTOJ 1324: Base64 Coding(Java)未解决,求题解

    题目链接:1324: Base64 Coding 资料:ASCII码表 原文是英文,而且篇幅较长.因此下面不粘贴原文,只写中文大意. Description Base64是一种编码算法.它的工作原理是 ...

  7. WEB学习路线2019完整版(附视频教程+网盘下载地址)

    WEB学习路线2019完整版(附视频教程+网盘下载地址).适合初学者的最新WEB前端学习路线汇总! 在当下来说web前端开发工程师可谓是高福利.高薪水的职业了.所以现在学习web前端开发的技术人员也是 ...

  8. spring整合MQ

    ---恢复内容开始--- 一. 导入依赖 <dependencies> <!-- ActiveMQ客户端完整jar包依赖 --> <dependency> < ...

  9. 将网站升级为https并自动续期Https证书。

    Let's Encrypt Let's Encrypt 是一个由Internet Security Research Group (互联网安全研究组)提供的免费,自动化和开放的证书颁发机构. 它秉承着 ...

  10. bootstrap环境搭建

    Bootstrap 是stwitter公司的两名前端设计师设计的基于html css javascript的超强的前端框架. Bootstrap 是一移动设备为优先,pc机,平板,手机皆适用的框架. ...