https://leetcode.com/problems/accounts-merge/description/

class UnionFound {
public:
unordered_map<int,int> parents;
int cnt = ;
void AddItem(int i) {
parents[i] = i;
cnt++;
}
int GetParent(int i) {
if (parents[i] == i) return i;
return parents[i] = GetParent(parents[i]);
}
void Union(int p, int c) {
int pp = GetParent(p);
int cp = GetParent(c);
if (pp != cp) {
parents[cp] = pp;
cnt--;
}
}
};
class Solution {
public:
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
UnionFound uf;
for (int i = ; i < accounts.size(); i++)
uf.AddItem(i); unordered_map<string, int> map_email_to_person;
for (int i = ; i < accounts.size(); i++)
for (int j = ; j < accounts[i].size(); j++) {
const auto& email = accounts[i][j];
if (map_email_to_person.count(email)) {
uf.Union(map_email_to_person[email], i);
}
else {
map_email_to_person[email] = i;
}
} unordered_map<int, set<string>> map_person_to_emails;
for (int i = ; i < accounts.size(); i++) {
int person = uf.GetParent(i);
for (int j = ; j < accounts[i].size(); j++) {
const auto& email = accounts[i][j];
map_person_to_emails[person].insert(email);
}
} vector<vector<string>> res;
for (const auto& it : map_person_to_emails) {
vector<string> p;
p.push_back(accounts[it.first][]);
for (const auto & email : it.second)
p.push_back(email);
res.push_back(p);
}
return res;
}
};

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 accounts, each element accounts[i] is a list of strings, where the first element accoun ...

  4. 721. Accounts Merge合并电子邮件账户

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

  5. LeetCode 721. Accounts Merge

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

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

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

  7. [LeetCode] Accounts Merge 账户合并

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

  8. [Swift]LeetCode721. 账户合并 | Accounts Merge

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

  9. [LeetCode] 721. Accounts Merge_Medium tag: DFS recursive

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

随机推荐

  1. AOP的XML实现方式

    与注解方式类似,只不过所有设置是通过xml来设置 // 切面类 public class Aop { public void around(ProceedingJoinPoint pjp) throw ...

  2. JMeter tomcat测试请求

    JMeter tomcat测试请求 Apache Jmeter是开源的压力测试工具,可以测试tomcat 的吞吐量等信息 下载地址: http://jmeter.apache.org/download ...

  3. (转)JSP HTML JAVASCRIPT 中文乱码 解决方案 大全

    JSP HTML JAVASCRIPT 中文乱码 解决方案 大全 JSP的中文字符一直是各位初学者首先要解决的问题,下面进行了总结,也给出了解决办法.C4.1 HTML中文编码转换 在JSP文件中的静 ...

  4. WCF 内置绑定在不同的传输安全模式下的信道层

    basicHttpBinding Transport安全模式信道层 Message安全模式信道层 TransportWithMessageCredential安全模式信道层 TransportCred ...

  5. 项目上传至Github

    到https://github.com/ 注册用户,然后点 Start a project,创建仓库 记住这个 地址. 再去 https://git-scm.com/downloads 下载git 安 ...

  6. tomcat7 开机自启动(转)

    转自 http://blog.csdn.net/rainyspring4540/article/details/51861079 环境:win7  tomcat7 开机自启动: 使用管理员打开命令提示 ...

  7. C#中Dictionary泛型集合7种常见的用法

    要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib)  Dictionary的描述1.从一组键(Key)到一组值( ...

  8. C#利用WebClient 两种方式下载文件(一)

    WebClient client = new WebClient(); 第一种 string URLAddress = @"http://files.cnblogs.com/x4646/tr ...

  9. 【Android开发笔记】生命周期研究

    启动 onCreate onStart onResume 退出键 onPause onStop onDestroy 锁屏 & 按住 home键 & 被其他Activity覆盖(Sing ...

  10. HDU3577 线段树(区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3577 ,普通的线段树区间更新题目,较简单. 相当于一个区间覆盖问题,有一点要注意的就是叶子节点是一个长 ...