Accounts Merge
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的更多相关文章
- 【LeetCode】721. Accounts Merge 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/accounts ...
- [LeetCode] Accounts Merge 账户合并
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- [Swift]LeetCode721. 账户合并 | Accounts Merge
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- [leetcode]721. Accounts Merge账户合并
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- 721. Accounts Merge合并电子邮件账户
[抄题]: Given a list accounts, each element accounts[i] is a list of strings, where the first element ...
- [LeetCode] 721. Accounts Merge 账户合并
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- LeetCode 721. Accounts Merge
原题链接在这里:https://leetcode.com/problems/accounts-merge/ 题目: Given a list accounts, each element accoun ...
- 【leetcode】721. Accounts Merge(账户合并)
Given a list of accounts where each element accounts[i] is a list of strings, where the first elemen ...
- 721. Accounts Merge
https://leetcode.com/problems/accounts-merge/description/ class UnionFound { public: unordered_map&l ...
随机推荐
- Python类的访问限制
使用两个_将变量设置为private,访问变量可定义get方法,对变量值修改可定义set方法,修改变量值的时候可检查参数的有效性. class Student(object): #定义一个Studen ...
- Python--遍历文件夹下所有文件和目录的方法(os.walk(rootdir)函数返回一个三元素元祖)
import os import os.path # This folder is custom rootdir = '/Users/macbookpro/Desktop/test' for pare ...
- CH01基于Ubuntu系统的ZYNQ-7000开发环境的搭建
1.1概述 实验环境: Windows 10 专业版 Vmware workstation 14.1.1 Ubuntu 16.04.3 Xilinx SDx 2017.4 实验内容: 国内Window ...
- ArcGIS SOE开发异常之 ClassFactory cannot supply requested class
最近SOE开发一个功能,辛辛苦苦写完, 异常: ClassFactory cannot supply requested class 辛苦解决: 百度一下,描述这个问题的帖子很多,不过内容基本一致.大 ...
- CCF 2017-03-1 分蛋糕
CCF 2017-03-1 分蛋糕 题目 问题描述 小明今天生日,他有n块蛋糕要分给朋友们吃,这n块蛋糕(编号为1到n)的重量分别为a1, a2, -, an.小明想分给每个朋友至少重量为k的蛋糕.小 ...
- python 内置函数和匿名函数
内置函数 截止到python版本3.6.2,现在python一共为我们提供了68个内置函数. Built-in Functions abs() dict() help() min() ...
- SAP Marketing Cloud功能简述(一) : Contacts和Profiles
Dong Grace是Jerry的同事,目前是SAP成都研究院数字创新空间团队的一名实习生,明年7月才毕业.这篇文章是Grace对SAP Marketing Cloud功能梳理的一篇学习笔记,因为篇幅 ...
- 铰链joints
Fixed Joint原理像阶层里的父子结构.关节会将对象锁在一个世界坐标或者锁在一个连接的刚体. 固定关节可以设定断裂力道(Break Farce)和断裂扭力(Break torque),破坏关节所 ...
- Shell 编程中的常用工具
文件查找 find 命令 语法格式 find命令总结: 常用选项: -name 查找/etc目录下以conf结尾的文件 find /etc -name "*.conf" -inam ...
- 支付宝手机网站支付(基于Java实现支付宝手机网站支付)
支付宝支付核心需要的参数是(APPID,PRIVATE_KEY,ALIPAY_PUBLIC_KEY) APPID:创建应用后就有的APPID. PRIVATE_KEY:应用私钥 ALIPAY_PUBL ...