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. Java修饰符/关键字

    修饰符分类: 权限修饰符:public.protected.default.private 其他修饰符:abstract.static.final.transient.volatile.native. ...

  2. 禁止form重复提交

    $("form").submit(function () { console.log("提交了"); $("input:submit").a ...

  3. orcale函数

    字符函数    1.ASCII 返回与指定的字符对应的十进制数;  select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ') space ...

  4. webpack.config.js====引入Jquery库文件

    1. 安装 cnpm install --save jquery expose-loader 2. 在webpack.config.js中配置 Jquery库是使用的webpack的一个插件Provi ...

  5. 如何下载最新的固件到Pixhawk

    连接Pixhawk至电脑 当Mission Planner 已经安装至你的电脑上,使用micro USB数据线连接pixhawk到您的计算机上. 使用一个USB端口直接在您的计算机上,不要用USB集线 ...

  6. 微信小程序时间处理问题

    环境: 开发环境: 1. Mac OS 10.12.5 2. 微信Web开发者工具 v0.18.182200 测试环境: 1. iPhone 7 2. iOS 10.3.2 3. 微信 6.5.9 问 ...

  7. Yii2 widgets [mztest/yii2-widget-file-upload]

    Enjoy it. A widget for uploading files to your server. Github , Packagist Screenshots

  8. 安装percona工具包

    1.安装percona源 sudo yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona- ...

  9. VMware Workstation Pro 11、12 密钥

    11:1F04Z-6D111-7Z029-AV0Q4-3AEH8 12:5A02H-AU243-TZJ49-GTC7K-3C61N

  10. pat乙级1059

    1.c++ 位数不够前面补零: printf("04d", i); 位数不够前面补空格(右对齐): printf("4d", i); 位数不够后面补空格(左对齐 ...