给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails 表示该帐户的邮箱地址。

现在,我们想合并这些帐户。如果两个帐户都有一些共同的邮件地址,则两个帐户必定属于同一个人。请注意,即使两个帐户具有相同的名称,它们也可能属于不同的人,因为人们可能具有相同的名称。一个人最初可以拥有任意数量的帐户,但其所有帐户都具有相同的名称。

合并帐户后,按以下格式返回帐户:每个帐户的第一个元素是名称,其余元素是按顺序排列的邮箱地址。accounts 本身可以以任意顺序返回。

例子 1:

  1. Input:
  2. accounts = [["John", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]]
  3. Output: [["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'], ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]]
  4. Explanation:
  5. 第一个和第三个 John 是同一个人,因为他们有共同的电子邮件 "johnsmith@mail.com"
  6. 第二个 John Mary 是不同的人,因为他们的电子邮件地址没有被其他帐户使用。
  7. 我们可以以任何顺序返回这些列表,例如答案[['Mary''mary@mail.com'],['John''johnnybravo@mail.com'],
  8. ['John''john00@mail.com''john_newyork@mail.com''johnsmith@mail.com']]仍然会被接受。

注意:

  • accounts的长度将在[1,1000]的范围内。
  • accounts[i]的长度将在[1,10]的范围内。
  • accounts[i][j]的长度将在[1,30]的范围内。

思路:这道题是并查集的考查。但是对于具体的实现方法,还是要多多想想。我一开始想的是,将列表中,每组的每个邮箱的父亲设为第一个元素,也就是accounts[i][0]。但是因为有重名的原因,这样的话没法进行合并,因为例子中,显然第一个和第三个的John是一个人,而第二个John是另外一个人,所以好像邮箱才是用来区分人的元素。

如何找到并查集中所谓的父亲节点是主要的问题,既然不能用名字,那么我们就用每组邮箱中的第一个邮箱来作为这一组的上级(父亲)。

首先我们需要初始化,每个邮箱的父亲是自己,而且还要存储一下每组的拥有者,就是accounts[i][0]。然后遍历,将每一组的邮箱的父亲都设为该组的第一个邮箱。接着利用map来操作(主要利用它的唯一性)map<string, set<string>>,第一个用来存祖宗,第二个用来存该祖宗下的所有子节点(即第一个存的是祖宗邮箱,第二个存的是祖宗是该邮箱的所有邮箱,包括它本身),遍历每个邮箱,将邮箱插入到map中。这里我们注意,虽然是个简单的插入过程,但是,实际的操作是:如果map中存在这个祖宗节点,那么将该邮箱直接插入到其后面的子集中,如果不存在,将该邮箱的祖宗以及自己本身放到map中。

为什么又出来个祖宗节点呢,因为每组的父亲只是第一个邮箱,如果该邮箱出现在其他组的子节点处,那么从整个局势上看,最后要以祖宗划山头的,自己不是祖宗,自己要带着自己的子节点,全部投奔祖宗去。所以每次放到map中时,同样要寻根。才能放入。

最后要将拥有者加进去才符合输出的规范。

  1. string find(string s, map<string, string> &p)//递归寻根
  2. {
  3. return p[s] == s ? s : find(p[s], p);
  4. }
  5. vector<vector<string>> accountsMerge(vector<vector<string>>& accounts)
  6. {
  7. map<string, string> owner; // map from the 邮箱 to 名字
  8. map<string, string> parents; // map from an 邮箱 to its 邮箱的父亲
  9. map<string, set<string>> unions; // the unions of accounts 并查集string下的结点们
  10. for (int i = 0; i < accounts.size(); ++i) { //初始化
  11. for (int j = 1; j < accounts[i].size(); ++j) {
  12. parents[accounts[i][j]] = accounts[i][j]; // 初始化自己是父亲
  13. owner[accounts[i][j]] = accounts[i][0]; //保存一下每个邮箱的拥有者
  14. }
  15. }
  16. for (int i = 0; i < accounts.size(); ++i) { // find and union 查找与合并
  17. string ancestor = find(accounts[i][1], parents);
  18. for (int j = 2; j < accounts[i].size(); ++j) {
  19. parents[find(accounts[i][j], parents)] = ancestor;
  20. }
  21. }
  22. for (int i = 0; i < accounts.size(); ++i) { //将每个邮箱结点放入它的根节点带领的并查集中
  23. for (int j = 1; j < accounts[i].size(); ++j) {
  24. unions[find(accounts[i][j], parents)].insert(accounts[i][j]);
  25. }
  26. }
  27. vector<vector<string>> res;
  28. map<string, set<string>>::iterator p=unions.begin();
  29. for (;p!=unions.end();p++) { //取出每一个map对
  30. vector<string> emails(p->second.begin(), p->second.end());
  31. emails.insert(emails.begin(), owner[p->first]); //在emails.begin()前面插入owner[p.first]元素
  32. res.push_back(emails);
  33. }
  34. return res;
  35. }

还有一种方案,就是将每组邮箱的行号作为父亲,这样更加简洁。

  1. vector<int> f;
  2. vector<int> r;
  3. int findF(int x)//找父亲节点
  4. {
  5. while (f[x] != x)
  6. x = f[x];
  7. return x;
  8. }
  9. void merge(int x, int y)//合并,如果两个节点的祖宗相同,返回,如果不同,要归入一个祖宗门下
  10. {
  11. int fx = findF(x);
  12. int fy = findF(y);
  13.  
  14. if (fx == fy)
  15. return;
  16.  
  17. if (r[fx] == r[fy])
  18. r[fx]++;
  19.  
  20. if (r[fx] > r[fy])
  21. f[fy] = fx;
  22. else
  23. f[fx] = fy;
  24. }
  25. vector<vector<string>> accountsMerge(vector<vector<string>>& accounts)
  26. {
  27. int n = accounts.size();
  28. for (int i = 0; i < n; i++)
  29. {
  30. f.push_back(i);//初始化每组的父亲为行号
  31. r.push_back(1);
  32. }
  33.  
  34. map<string,int> m;//m 从邮箱到行号的映射
  35. vector<vector<string>> ret;
  36. if (n == 0)
  37. return ret;
  38.  
  39. for (int i = 1; i < accounts[0].size(); i++)
  40. {
  41. m[accounts[0][i]] = 0;
  42. }
  43.  
  44. for (int i = 1; i < n; i++)
  45. {
  46. for (int j = 1; j < accounts[i].size(); j++)
  47. {
  48. if (m.find(accounts[i][j]) != m.end())//如果m中存在这个邮箱
  49. {
  50. merge(m[accounts[i][j]], i);
  51. }
  52. else
  53. m[accounts[i][j]] = i;//如果不存在,插入
  54. }
  55. }
  56. map<string,int>::iterator it;
  57. map<int, vector<string>> km;
  58. for (it = m.begin(); it != m.end(); it++)
  59. {
  60. int k = findF(it->second);
  61. if (km.find(k) == km.end())
  62. km[k].push_back(accounts[k][0]);
  63. km[k].push_back(it->first);
  64. }
  65.  
  66. map<int, vector<string>>::iterator it2;
  67. for (it2 = km.begin(); it2 != km.end(); it2++)
  68. {
  69. ret.push_back(it2->second);
  70. }
  71. return ret;
  72. }

Leetcode(712)-账户合并的更多相关文章

  1. Java实现 LeetCode 721 账户合并(并查集)

    721. 账户合并 给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails ...

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

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

  3. 每日一道 LeetCode (19):合并两个有序数组

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  4. [LeetCode] Accounts Merge 账户合并

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

  5. [LeetCode] 721. Accounts Merge 账户合并

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

  6. [leetcode]721. Accounts Merge账户合并

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

  7. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  8. LeetCode编程训练 - 合并查找(Union Find)

    Union Find算法基础 Union Find算法用于处理集合的合并和查询问题,其定义了两个用于并查集的操作: Find: 确定元素属于哪一个子集,或判断两个元素是否属于同一子集 Union: 将 ...

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

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

随机推荐

  1. [XAML] 使用 XAML 格式化工具:XAML Styler

    1. XAML 的问题 刚入门 WPF/UWP 之类的 XAML 平台,首先会接触到 XAML 这一新事物.初学 XAML 时对它的印象可以归纳为一个词:一坨. 随着我在 XAML 平台上工作的时间越 ...

  2. 接收的参数为日期类型、controller控制层进行数据保存、进行重定向跳转

    目录 1.接收的参数为日期类型 2.controller控制层进行数据保存 3.controller层如何进行重定向跳转(因为默认是请求转发) 4.静态资源的映射 1.接收的参数为日期类型 WEB-I ...

  3. Mybatis解决字段与属性不匹配的问题、链表查询、嵌套查询、#{}和${}的区别

    1.使用接口结合xml映射文件 创建一个接口,该接口要和映射文件匹配(接口中方法名要和映射文件中的id相同) 映射文件中命名空间要和接口全类名相同 测试: 创建一个与src同级的源文件夹resourc ...

  4. 参数模型检验过滤器 .NetCore版

    最近学习 .NETCore3.1,发现过滤器的命名空间有变化. 除此以外一些方法的名称和使用方式也有变动,正好重写一下. 过滤器的命名空间的变化 原先:System.Web.Http.Filters; ...

  5. Pusher Channels Protocol | Pusher docs https://pusher.com/docs/channels/library_auth_reference/pusher-websockets-protocol

    Pusher Channels Protocol | Pusher docs https://pusher.com/docs/channels/library_auth_reference/pushe ...

  6. c 越界 数组越界

    int main(int argc, char* argv[]){ int i = 0; int arr[3] = {0}; for(; i<=3; i++){ arr[i] = 0; prin ...

  7. Azure Terraform(八)利用Azure DevOps 实现Infra资源和.NET CORE Web 应用程序的持续集成、持续部署

    一,引言 上一篇讲解到利用 Azure DevOps 将整个 Azure Web App,Azure Traffic Manager profile,Azure Storage Account,Azu ...

  8. Prometheus+Grafana+kafka_exporter监控kafka

    Prometheus+Grafana+kafka_exporter搭建监控系统监控kafka 一.Prometheus+Grafana+kafka_exporter搭建监控系统监控kafka 1.1K ...

  9. Jenkins安装部署项目

    Jenkins安装部署项目 配置JDK git maven 部署到服务器 一.新建任务 二.配置jenkins 三.添加构建信息 四.应用.保存 五.踩坑填坑记录 5.1没有jar包的情况 5.2无法 ...

  10. c++复习笔记(2)

    1. 类与对象 类的声明与结构,数据成员和成员函数. 成员函数可以在类外被定义.但是必须在类内声明. 封装:protect--允许类成员和派生类成员访问. 构造函数之外,还有一种初始化类成员的方法:参 ...