721. Accounts Merge合并电子邮件账户
[抄题]:
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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么用hashmap来做union find:
多用几个hashmap,用一个parents来存第一个邮箱,用一个owner来存用户,用一个union来存关系
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
为了排序,把每组的第一个邮箱都当作该组的parent
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 忘记写find函数了:union find题目必须写的吧
- 第一位邮件的parent就是自身,所以从第二位开始处理parent
- 往map的tree里加东西包括:得到树+往树里加东西两步 不涉及什么图不图的
unions.get(p).add(a.get(i))
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
union find中为了排序必须要有parent的哈希表
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
684. Redundant Connection 去掉一条边后变成树
[代码风格] :
[是否头一次写此类driver funcion的代码] :
class Solution {
public List<List<String>> accountsMerge(List<List<String>> accounts) {
//cc
List<List<String>> results = new ArrayList<List<String>>();
if (accounts == null || accounts.size() == 0) return results; //ini: 3 hashmap, put into parents owner
HashMap<String, String> parents = new HashMap<>();
HashMap<String, String> owners = new HashMap<>();
HashMap<String, TreeSet<String>> unions = new HashMap<>();
for (List<String> a : accounts) {
for (int i = 1; i < a.size(); i++) {
parents.put(a.get(i), a.get(i));
owners.put(a.get(i), a.get(0));
}
} //renew parents
for (List<String> a : accounts) {
String p = find(a.get(1), parents);
for (int i = 2; i < a.size(); i++) {
parents.put(find(a.get(i), parents), p);
}
} //put into unions to order
for (List<String> a : accounts) {
String p = find(a.get(1), parents);
if (!unions.containsKey(p)) unions.put(p, new TreeSet());
for (int i = 1; i < a.size(); i++) {
unions.get(p).add(a.get(i));
}
} //add to res
for (String a : unions.keySet()) {
List<String> result = new ArrayList<String>(unions.get(a));
result.add(0, owners.get(a));
results.add(result);
} //return
return results;
} public String find(String s, HashMap<String, String> map) {
return map.get(s) == s ? s : find(map.get(s), map);
}
}
721. Accounts Merge合并电子邮件账户的更多相关文章
- 【LeetCode】721. Accounts Merge 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/accounts ...
- [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(账户合并)
Given a list of accounts where each element accounts[i] is a list of strings, where the first elemen ...
- [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 ...
- 721. Accounts Merge
https://leetcode.com/problems/accounts-merge/description/ class UnionFound { public: unordered_map&l ...
- STL源代码分析——STL算法merge合并算法
前言 因为在前文的<STL算法剖析>中.源代码剖析许多.不方便学习.也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的merge合并算法. ...
- Oracle merge合并更新函数
本博客介绍一下Oracle merge合并函数,业务场景:新增数据的时候要先查询数据库是否已经有改数据,有数据就更新数据,没数据才新增数据,这是很常见的业务场景,如果是用Oracle数据库的话,其实直 ...
- Egit的merge合并冲突具体解决方法
稍微总结下弄了半个下午的egit的merge合并冲突解决方法,网上看的都是一个模板出来的,看的糊里糊涂,花了很多时间去实验整个合并流程.. 前提工作 创建一个普通JAVA工程Test,创建一个类Tes ...
随机推荐
- python 读写三菱PLC数据,使用以太网读写Q系列,L系列,Fx系列的PLC数据
本文将使用一个gitHub开源的组件技术来读写三菱的plc数据,使用的是基于以太网的TCP/IP实现,不需要额外的组件,读取操作只要放到后台线程就不会卡死线程,本组件支持超级方便的高性能读写操作 gi ...
- MyBatis的一对一
1. 建立好工程后,在pom.xml中配置myBatis的依赖. <project xmlns="http://maven.apache.org/POM/4.0.0" xml ...
- Anatoly and Cockroaches
Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also li ...
- test20181029 数列
题意 分析 考场做法 打表发现,最后的循环节一定是\(\gcd(a_1,a_2),\gcd(a_1,a_2),0\)这种形式,而稍微思考一下便知道这显然是一般情况. 然后都有gcd了,发现操作的实质都 ...
- 有些文件不需要配置,只需要放到resources下面
今天和一位同事探讨了一个问题,开始的时候我事先的读取某个映射文件是配置在applicationContext.xml的bean定义里面:但是他提出来,是否可以不需要配置呢?直接作为一个资源文件完事,这 ...
- 开始SDK之旅-入门1基本环境搭建与测试
已验证这个可用. http://bbs.ccflow.org/showtopic-2560.aspx 集成方式已经用一段时间了,今天刚好有时间,尝试下SDK.使用的话,也很方便,以下是简单的步骤1.新 ...
- bzoj2337 XOR和路径
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2337 首先:因为是异或和,所以可以考虑每一位考虑. 就在每一位上求一下该位是1的概率,乘以1 ...
- Spring Cloud 入门 之 Ribbon 篇(二)
原文地址:Spring Cloud 入门 之 Ribbon 篇(二) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Cloud 入门 之 Eureka ...
- ASP.NET 迭代控件获得行号
如何获取Repeater的当前行号,其实Repeater自身就带有这个获取当前行号的属性,而无需程序员绑定这个行号.到底要怎么实现呢?其实使用Repeater中的 Container.ItemInde ...
- php 面试一般都遇到什么问题
大型互联网公司会从几个方面来考核:第一:专业上,专业分为五个方向,操作系统,网络,算法,语言,数据库,一般情况下,会比较在乎Linux系统的日常使用,包括shell脚本,比较深入的话,会问kernel ...