[抄题]:

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. Scrum冲刺博客(总)

    一.在 Alpha 阶段认领的任务 二.明日任务安排 成员 任务 任务量/小时 陈伟泽 项目进度把控 1 李嘉廉 24点算法" Stack数据结构实现,生成不重复的算式(考虑结合律和结合律) ...

  2. MAC远程桌面连接问题

    如图,始终提示“证书或相关链无效.” 点击“取消” 点击“编辑连接...” 点击“打开” 点击“安全性” 选择“即使验证失败,也始终连接” OK

  3. Jenkins搭建windows service自动编译发布环境

    类库项目(Task)部署 前面搭建了Web站点的环境,类库项目发布不同于站点项目,它只需要将MSBuild编译出来的dll复制到目标服务器上即可,而不需要通过Web Deploy,下面来说一下如何发布 ...

  4. C#模板引擎 DotLiquid

    DotLiquid 是一个简单.快速和安全的模板引擎,移植自 Ruby 的 Liquid 标签. 示例模板: <p>{{ user.name }} has to do:</p> ...

  5. C语言多线程pthread库相关函数说明

    线程相关操作说明 一 pthread_t pthread_t在头文件/usr/include/bits/pthreadtypes.h中定义: typedef unsigned long int pth ...

  6. 适合Centos Web服务器的iptables规则

    适合Centos Web服务器的iptables规则IPT="/sbin/iptables"$IPT --delete-chain$IPT --flush$IPT -P INPUT ...

  7. POJ1236学校网络——tarjan

    题目:http://poj.org/problem?id=1236 Tarjan+缩点.温习一下Tarjan的写法. 1.在缩点后的TAG中,有几个联通块等价于有几个入度为0的点! 2.把它们都联通相 ...

  8. Appcan、apicloud、HBuilder 不同之处解析

    来源:http://www.mamicode.com/info-detail-1129829.html 现在Hybrid app是一中非常火热的开发模式,在国内对应的开发工具也乱象丛生,有WeX5.c ...

  9. 黄聪:wordpress向mysql字段中保存html代码(使用add_option()方法),然后无法显示出问题

    你可以把" 引号去掉了再进库,或者使用 stripslashes_deep() <?php $str = "Is your name O\'reilly?"; // ...

  10. [模板]ST表浅析

    ST表,稀疏表,用于求解经典的RMQ问题.即区间最值问题. Problem: 给定n个数和q个询问,对于给定的每个询问有l,r,求区间[l,r]的最大值.. Solution: 主要思想是倍增和区间d ...