721. Accounts Merge
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的更多相关文章
- 【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 accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- 721. Accounts Merge合并电子邮件账户
[抄题]: Given a list accounts, each element accounts[i] is a list of strings, where the first element ...
- LeetCode 721. Accounts Merge
原题链接在这里:https://leetcode.com/problems/accounts-merge/ 题目: Given a list accounts, each 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] Accounts Merge 账户合并
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- [Swift]LeetCode721. 账户合并 | Accounts Merge
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- [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 ...
随机推荐
- 【转】《Unity Shader入门精要》冯乐乐著 书中彩图
为方便个人手机学习时候查阅,从网上转来这些彩图. 如属过当行为,联系本人删除. 勘错表 http://candycat1992.github.io/unity_shaders_book/unity_s ...
- phpcms文件结构
主要目录部分 /admin 管理后台目录 -- /skin/ 后台样式 -- /templates/ 后台样式模板 /api api接口 /corpandresiz ...
- DBTransaction
// Created by 张国锋 on 15-7-23. // Copyright (c) 2014年 张国锋. All rights reserved. // #import "AppD ...
- 自定义列表dl
语法格式 <dl> <dt>名词1</dt> <dd>名词1解释1</dd> <dd>名词1解释2</dd> ... ...
- ngnix入门配置
文件1.首先到ngnix下载页面下载你操作系统对应的ngnix压缩包 http://nginx.org/en/download.html 博主我是window10操作系统 上面是我解压之后放的 ...
- Don't let anyone tell you different.
Don't let anyone tell you different.不要让任何人否定你的与众不同.
- hibernate笔记4--qbc查询
Criteria:是一种完全面向对象的查询方式,Criteria查询也叫做qbc查询(query by Criteria). 查询全部,分页查询,统计查询,条件查询,排序查询,离线查询 ...
- Notification高级技巧
观察Notification这个类,你会发现里面还有很多我们没有使用过的属性.先来看看sound这个属性吧,它可以在通知发出的时候播放一段音频,这样就能够更好地告知用户有通知到来.sound 这个属性 ...
- 【C++】【MFC】创建新的线程函数
DWORD WINAPI MyThreadProc (LPVOID lpParam){ somestruct* pN = (somestruct*)lpParam; // 将参数转为你的类型 ... ...
- c++ STL map容器成员函数
map容器用于查找,设置键值和元素值,输入键值,就能得到元素值.map对象中的元素时刻都是有序的,除非无序插入的.它是用平衡树创建的.查找很快. 函数 描述,注意有r的地方都是不能用it代替的. ma ...