【leetcode】721. Accounts Merge(账户合并)
Given a list of accounts where each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account.
Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some common email to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.
After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.
Example 1:
Input: accounts = [["John","johnsmith@mail.com","john_newyork@mail.com"],["John","johnsmith@mail.com","john00@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]
Output: [["John","john00@mail.com","john_newyork@mail.com","johnsmith@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]
Explanation:
The first and second John's are the same person as they have the common email "johnsmith@mail.com".
The third John and Mary are different people as none of their email addresses are used by other accounts.
We could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'],
['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.
这道题的需求是账户合并,accounts 中每个数组中第一个元素是用户名,后面是其邮箱,最终需要合并求出一个用户下所有的邮箱,这里要注意以下,相同的用户名所属的邮箱不一定合并,除非其用户名下有相同的邮箱。
有一种初略的合并方法,先用set存储一个用户的所有邮箱,然后遍历另外一个同名用户中所有的邮箱,如果在之前用户存储邮箱数组中find到,那么就insert到前一个set中,但是这样会存在一个问题。即
结果发现对于下面这个例子不适用:
["John", "a@gmail.com", "b@gmail.com"]
["John", "c@gmail.com", "d@gmail.com"]
["John", "a@gmail.com", "c@gmail.com"]
如果按照遍历顺序不同,第二组的邮箱并不会加入到“John”中,也就是上述融合思路无法传递依赖,对于这种分组问题,还是优先用并查集来做,先分好组,每个邮箱指向一个根邮箱,然后根据根邮箱来找用户,存储下来。所以按照这种思路,那么我们需要构建如下几个数据结构,root 用于映射根邮箱,owner用于邮箱与用户的映射,还需要一个map 来去重的存储根邮箱以及同一组邮箱。根据上述分析代码如下:
class Solution {
public:
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
// 用什么数据结构最方便的存储这些数据
// 用map 记录用户与 邮箱的映射 邮箱用set 存储去重?
// 重点是用什么样的数据结构对上述accounts进行存储 分组
// 涉及到分组问题 可以考虑用union find进行分组
vector<vector<string>> res;
unordered_map<string,string> root;// 每个邮箱指向的根
unordered_map<string,string> owner;// 每个邮箱指向的用户
unordered_map<string,set<string>>mm;// 一个根邮箱对应的一组相关联的邮箱
// 初始化并查集的root
for(auto account:accounts){
for(int i=1;i<account.size();++i){
// 第一个元素是用户名
root[account[i]]=account[i];// 每个邮箱开始都指向自己
owner[account[i]]=account[0];
}
}
// 同组邮箱分类
for(auto account:accounts){
string p=find(account[1],root);
for(int i=2;i<account.size();++i){
root[find(account[i],root)]=p;//union find 更新根节点
}
}
// 同一组邮箱汇总
for(auto account:accounts){
for(int i=1;i<account.size();++i){
mm[find(account[i],root)].insert(account[i]);
}
}
//将同组邮箱 根据根节点映射寻找owner
for(auto m:mm){
vector<string> vv(m.second.begin(),m.second.end());
vv.insert(vv.begin(),owner[m.first]);//在数组开头插入用户名
res.push_back(vv);
}
return res;
}
string find(string s,unordered_map<string,string> &root){
return s==root[s]?s:(root[s]=find(root[s],root));// 压缩路径并返回每个邮箱映射的根节点
}
};
【leetcode】721. Accounts Merge(账户合并)的更多相关文章
- [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 ...
- [LeetCode] Accounts Merge 账户合并
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- LeetCode 721. Accounts Merge
原题链接在这里:https://leetcode.com/problems/accounts-merge/ 题目: Given a list accounts, each element accoun ...
- 【LeetCode】721. Accounts Merge 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/accounts ...
- 721. Accounts Merge合并电子邮件账户
[抄题]: Given a list accounts, each element accounts[i] is a list of strings, where the first element ...
- Leetcode(712)-账户合并
给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails 表示该帐户的邮箱地址 ...
- [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 ...
- 721. Accounts Merge
https://leetcode.com/problems/accounts-merge/description/ class UnionFound { public: unordered_map&l ...
随机推荐
- VNC服务器的搭建(带图形化支持)
环境:centos7.6最小化安装 图形化支持 如果希望安装简单的图形支持的话,仅包含gnome的最最最最基础的包的话可以使用以下命令 yum groups install "X Windo ...
- Dataworks批量刷数优化方案探讨
Dataworks批量刷数优化方案探讨 在数据仓库的日常使用中,经常会有批量补数据,或者逻辑调整后批量重跑数据的场景. 批量刷数的实现方式,因调度工具差异而各有不同. Dataworks调度批量刷数局 ...
- Loto实践干货(8) 实测 保险丝 用示波器带电流探头
本文用LOTO示波器和5A的电流探头来实验两种常见类型的保险丝的保护曲线.一种是熔断型的,另一种是自恢复型的.我们通常需要在一些电路中对电流过大的情况做保护,比如防止用户把输出源短路,比如防止用户对电 ...
- 计算机网络漫谈之OSI七层模型和TCP/IP四层模型
在 什么是网络? 中,你已经知道计算机网络是物理连接的"局域网"和工作于这个局域网上的"网络协议",并且我们的重心是网络协议.有关网络协议,按照目前的分层方式主 ...
- MySQL 查询语句(1)
一:创建数据库 1:CREATE DATABASE test; //创建数据库test 2:SHOW DATABASES: //查看目前数据库中可用的数据库,默认会有系统数据库 3:USE test; ...
- Integer.valueOf()和Integer.parseInt()区别
他们返回类型的不同是最大的原因. static int parseInt(String s) 将字符串参数作为有符号的十进制整数进行分析. static Integer valueOf(int i) ...
- 问题 F: 背包问题
题目描述 现在有很多物品(它们是可以分割的),我们知道它们每个物品的单位重量的价值v和重量w(1<=v,w<=10):如果给你一个背包它能容纳的重量为m(10<=m<=20), ...
- 菜鸡的Java笔记 第五 - java 程序逻辑控制
程序主要分为三种逻辑:顺序,分支,循环. if 分支语句 if分支语句是最为基础的分支操作,但是其有三种使用形式: if语句 if.....else 语句 if....else...if...el ...
- RabbitMQ Network Partitions 处理策略
欢迎支持笔者新作:<深入理解Kafka:核心设计与实践原理>和<RabbitMQ实战指南>,同时欢迎关注笔者的微信公众号:朱小厮的博客. 网络分区的意义 RabbitMQ的模型 ...
- .NET 百万级 大数据插入、更新 ,支持多种数据库
功能介绍 (需要版本5.0.44) 大数据操作ORM性能瓶颈在实体转换上面,并且不能使用常规的Sql去实现 当列越多转换越慢,SqlSugar将转换性能做到极致,并且采用数据库最佳API 操作数据库 ...