[抄题]:

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

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 忘记写find函数了:union find题目必须写的吧
  2. 第一位邮件的parent就是自身,所以从第二位开始处理parent
  3. 往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合并电子邮件账户的更多相关文章

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

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

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

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

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

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

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

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

  5. LeetCode 721. Accounts Merge

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

  6. 721. Accounts Merge

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

  7. STL源代码分析——STL算法merge合并算法

    前言 因为在前文的<STL算法剖析>中.源代码剖析许多.不方便学习.也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的merge合并算法. ...

  8. Oracle merge合并更新函数

    本博客介绍一下Oracle merge合并函数,业务场景:新增数据的时候要先查询数据库是否已经有改数据,有数据就更新数据,没数据才新增数据,这是很常见的业务场景,如果是用Oracle数据库的话,其实直 ...

  9. Egit的merge合并冲突具体解决方法

    稍微总结下弄了半个下午的egit的merge合并冲突解决方法,网上看的都是一个模板出来的,看的糊里糊涂,花了很多时间去实验整个合并流程.. 前提工作 创建一个普通JAVA工程Test,创建一个类Tes ...

随机推荐

  1. ubuntu16.04 tensorflow pip 方式安装

    首先,需要知道   tensorflow  1.5版本以上包括 1.5版本  的GPU类型都是需要安装  cuda9.0的,  tensorflow-gpu  1.4版本是可以使用cuda 8.0. ...

  2. Mysql的日期转换成星期[某天对应周几]

    |—— 应用中会有各种不同的需求,要灵活应对:比如拿到某一日期要知道是周几 |——DAYOFWEEK(date) [返回日期date的星期索引(1=星期天,2=星期一, ……7=星期六).这些索引值对 ...

  3. np.ones(N)/N的作用

    在python中导入numpy包 N=5 weights = np.ones(N)/N       //这里就相当于创建了一个数组,且为5个1/5的数组 print "weights&quo ...

  4. HDU2874Connections between cities( LCA )Tarjan

    Problem Description After World War X, a lot of cities have been seriously damaged, and we need to r ...

  5. SqlServer一些常用函数(持续更新。。。)

    1. 字符串拼接: + 拼接 SELECT 'AA' + 'BB' A //AABB在2012版本sqlserver之后,可以使用cancat进行字符串拼接了. 2. 判断是否为空,并取另外的值 :I ...

  6. Rotor envoy control plane 简单试用

    rotor 基于golang 的envoy xds 服务,支持多种集成方式: k8s consul aws dc/os demo试用docker 以及consul 进行环境运行 下载demo 可以试用 ...

  7. nats 学习 request/reply 模式基本使用

    nats 一个云原生的消息系统,使用简单,客户端丰富,支持的模式是pub/sub 但是集成比较灵活,可以支持loadblance, request/reply pub/sub 代码演示的是reques ...

  8. Java9的新特性

    2017.9.21延期了好几次的Java9正式发布,在人工智能的时代,java还能不能持续辉煌是个问题.看看java9的新特性没什么让自己想升级的意愿,因为要么时一些特性用不到,要么时已经有其它方案代 ...

  9. laravel 整合 swoole ,并简单 ab 测试对比性能以及在 PHPstorm 中利用debug调试配置swoole服务中的PHP代码

    安装PHP 的 swoole 扩展 及 安装 laravel,就不描述了 整合 laravel 和 swoole 用了这个轮子,侵入性很小,一行代码搞定,推荐一下,今天刚用,不能预测未来是否会遇见坑 ...

  10. hackerrank Week of Code 31

    https://www.hackerrank.com/contests/w31/challenges Beautiful Word 模拟 Accurate Sorting 检查每个数字距离原位是否都不 ...