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

  1. class UnionFound {
  2. public:
  3. unordered_map<int,int> parents;
  4. int cnt = ;
  5. void AddItem(int i) {
  6. parents[i] = i;
  7. cnt++;
  8. }
  9. int GetParent(int i) {
  10. if (parents[i] == i) return i;
  11. return parents[i] = GetParent(parents[i]);
  12. }
  13. void Union(int p, int c) {
  14. int pp = GetParent(p);
  15. int cp = GetParent(c);
  16. if (pp != cp) {
  17. parents[cp] = pp;
  18. cnt--;
  19. }
  20. }
  21. };
  22. class Solution {
  23. public:
  24. vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
  25. UnionFound uf;
  26. for (int i = ; i < accounts.size(); i++)
  27. uf.AddItem(i);
  28.  
  29. unordered_map<string, int> map_email_to_person;
  30. for (int i = ; i < accounts.size(); i++)
  31. for (int j = ; j < accounts[i].size(); j++) {
  32. const auto& email = accounts[i][j];
  33. if (map_email_to_person.count(email)) {
  34. uf.Union(map_email_to_person[email], i);
  35. }
  36. else {
  37. map_email_to_person[email] = i;
  38. }
  39. }
  40.  
  41. unordered_map<int, set<string>> map_person_to_emails;
  42. for (int i = ; i < accounts.size(); i++) {
  43. int person = uf.GetParent(i);
  44. for (int j = ; j < accounts[i].size(); j++) {
  45. const auto& email = accounts[i][j];
  46. map_person_to_emails[person].insert(email);
  47. }
  48. }
  49.  
  50. vector<vector<string>> res;
  51. for (const auto& it : map_person_to_emails) {
  52. vector<string> p;
  53. p.push_back(accounts[it.first][]);
  54. for (const auto & email : it.second)
  55. p.push_back(email);
  56. res.push_back(p);
  57. }
  58. return res;
  59. }
  60. };

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. drupal优化全攻略

    下面是drupal优化的一些经验.分四大部分来讲. 第一部分:Drupal系统本身的设置来达到优化 第二部分:针对php代码进行的优化 第三部分:针对MYSQL数据库进行的优化 第四部分:针对网站架构 ...

  2. pat1062. Talent and Virtue (25)

    1062. Talent and Virtue (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Li Abou ...

  3. EditPlus常用操作

    EditPlus注册码在线生成 http://www.jb51.net/tools/editplus/ 随意填写个用户名,生成对应的密码就可以使用editplus了 EditPlus常用快捷键 编代码 ...

  4. Android客户端与PC服务端、android服务端通过WiFi通信

    前期准备:我的是Linux Mint操作系统(总之折腾的过程中怀疑过是不是系统的问题),首先是要创建wifi热点给android手机使用,这个时候笔记本作为通信的服务器端,android手机作为客户端 ...

  5. Java图形界面开发—列出指定目录

    代码如下: package com.packageTemp; import javax.swing.*; import java.awt.*; import java.awt.event.*; imp ...

  6. Android使用文件管理器打开指定文件夹,浏览里面的内容

    Android下可以打开一些文件,带有.doc 等后缀的文件网上一般都有解释,这个写一个使用文件管理器打开指定文件夹的 private void openAssignFolder(String pat ...

  7. 跨平台移动开发phonegap/cordova 3.3全系列教程-目录

    目录(更新完成后会附上源码供参考) 第一章 android平台开发 phonegap/cordova简介 1.开发环境搭建 2.helloworld 3.启动画面 4.结合asp.net/jqmboi ...

  8. Unity光晕剑效果的Shader简单实现

    最近遇到了一个需求,想要一种在刀剑上带有光晕的酷炫效果,甚至是还想要有闪烁呼吸效果,于是就写了一个简单的叫LightSwrod的Shader去实现,先上图看看效果吧. 简单展示 这是剑本身的样子 这是 ...

  9. 初识Notification

    通知(Notification)是 Android 系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现.发出一条通知后,手机最上方 ...

  10. 约瑟夫环问题及PHP代码实现

    php实现猴子选大王 <?php /** * @param $n 猴子数量 * @param $m 出列的那个数 */ function king($n,$m){ $monkeys = rang ...