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 ...
随机推荐
- 工作中比较重要的经验分享-2016-bypkm
工作中总有一些经验能让人记忆深刻,能让人终生受用,相比技术而言,经验是宝贵的.无价的.在我的博客中,主要是技术类的博文,那些东西是相对死板的,价值也相对低廉.今天就记录一下我在工作中一次比较重要的经验 ...
- 项目代码matlab
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ...
- Git 将代码恢复到一个历史的版本
Git 将代码恢复到一个历史的版本 要把代码回到某个历史版本 比如 test有两种方法 暴力的方式 如果你的仓库是自己在用(不影响别人),那么你可以使用 git reset --hard <ta ...
- QLoo graphql engine了解
参考架构图 处理流程 使用gloo注册服务api 发现断电以及serverless 函数 更新graphql schema 在qloo的resolvermap 中连接schema定义的字段 特性 不用 ...
- RK3288 开机动画旋转
CPU:RK3288 系统:Android 5.1 如果开机动画与屏显示方向不一致,有两种方法可以更改开机动画方向. 一.RK3288默认的开机动画是由两张图片组合而成的,可以直接旋转两张图片的方向. ...
- Bootstrap-CL:面包屑导航
ylbtech-Bootstrap-CL:面包屑导航 1.返回顶部 1. Bootstrap 面包屑导航(Breadcrumbs) 面包屑导航(Breadcrumbs)是一种基于网站层次信息的显示方式 ...
- Ruby中数组的遍历
转自:http://www.jianshu.com/p/8de9b60f9350 Ruby中有几个常用的遍历数组的方法,本人经常容易搞混.这里列出来总结一下. each: 从数组中取出一个元素,作为某 ...
- AWT,Swing,RCP 开发
http://www.blogjava.net/youxia/category/17374.html
- nginx收到空包问题
tcpdump有收包,但是nginx的access.log显示post数据为空 可以通过tcpdump监控端口 http://www.cnblogs.com/linn/p/4792468.html 修 ...
- mysql迁移之巨大数据量快速迁移方案
mysql迁移之巨大数据量快速迁移方案-增量备份及恢复 --chenjianwen 一.前言: 当mysql库的大小达到几十个G或者上百G,迁移起来是一件非常费事的事情,业务中断,导出导入耗费大量的时 ...