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 ...
随机推荐
- java向上取整向下取整
向上取整用Math.ceil(double a) 向下取整用Math.floor(double a) 举例: public static void main(String[] args) throws ...
- ElasticSearch java API 按照某个字段排序
searchRequestBuilder.addSort("publish_time", SortOrder.DESC); 按照某个字段排序的话,hit.getScore()将会失 ...
- Educational Codeforces Round 51 (Rated for Div. 2)
做了四个题.. A. Vasya And Password 直接特判即可,,为啥泥萌都说难写,,,, 这个子串实际上是忽悠人的,因为每次改一个字符就可以 我靠我居然被hack了???? %……& ...
- vscode 常用插件安装
设置中文语言使用快捷键[Ctrl+Shift+P],弹出的搜索框中输入[configure language],然后选择搜索出来的[Configure Display Language],locale ...
- 关于IE和Firefox兼容性问题及解决办法
1.//window.eventIE:有window.event对象FF:没有window.event对象.可以通过给函数的参数传递event对象.如onmousemove=doMouseMove(e ...
- Java图形界面开发—简易记事本
在学习了Java事件之后,自己写了一个极其简单的记事本.用到了MenuBar,Menu,MenuITem等控件,事件包括ActionListener以及KeyListener. 代码如下: ...
- lintcode中等难度5道题
1.整数转罗马数字 对任一个罗马数字可以 由12个罗马字符进行加法操作完成,且大数在左,小数在右,可以将一个数字进行拆分来求解 2.买卖股票的最佳时机 II 可将问题转换为只要相连的两天prices[ ...
- SqlServer Alwayson 搭建排错记录(二)
下面记录下建立好alwayson可用性组后,向可用性组内添加数据库出现过的问题及解决方法 一.数据库未处于恢复状态 将数据库联接到可用性组的时候报错: 数据库“XXXX”未处于恢复状态,而此状态是镜像 ...
- tomcat jvm参数优化
根据gc(垃圾回收器)的选择,进行参数优化 JVM给了三种选择:串行收集器.并行收集器.并发收集器,但是串行收集器只适用于小数据量的情况,所以这里的选择主要针对并行收集器和并发收集器. -XX:+Us ...
- 关于VPS主机KVM/OPENVZ/CN2区别与含义
1.OPENVZ OpenVZ是基于Linux内核和作业系统的操作系统级虚拟化技术.OpenVZ允许物理服务器运行多个操作系统,被称虚拟专用服务器(VPS,Virtual Private Server ...