【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 ...
随机推荐
- 当src获取不到图片,onerror可指定一张默认的图片
<img src="img/789.png" onerror="javascript:this.src='img/123.png';" alt=" ...
- 关于iview、element-ui重置表单并清除校验的方法
平时在使用iview或者vue重置表单是时,我会习惯使用 this.$refs[formData].resetFields(); 但是直接这样写上去方法是不起作用的, 内容必须要在每个form-ite ...
- robotframework-ride快捷方式打不开
我安装的是最新的RIDE2.0属于beta测试中,覆盖了3.8但仍不支持3.9 我的安装环境如下: 安装ride成功,启动ride的时候遇到了如下问题: 一:AttributeError: No at ...
- JAVA学习(六)
今天先是把内存知识总结归纳地又学习了一遍,现在可以很清楚地描述JVM的内存是如何操作的了. 静态变量储存在方法区内存中,这个之前没有注意到,温故知新了. 如果一个引用是空的(就是指向null),那它在 ...
- JS中innerHTML、outerHTML、innerText 、outerText、value的区别与联系?
1.innerHTML 属性 (参考自<JavaScript高级程序设计>294页) 在读模式下,innerHTML 属性返回与调用元素的所有子节点(包括元素.注释和文本节点)对应的 HT ...
- 自由导入你的增量数据-根据条件将sqlserver表批量生成INSERT语句的存储过程实施笔记
文章标题: 自由导入你的增量数据-根据条件将sqlserver表批量生成INSERT语句的存储过程增强版 关键字 : mssql-scripter,SQL Server 文章分类: 技术分享 创建时间 ...
- Java 插入html字符串到PPT幻灯片
通过Java后端代码操作PPT幻灯片时,可直接在幻灯片中绘制形状,并在形状中添加文本字符串内容.本篇文章,介绍一种通过html字符串来添加内容到PPT幻灯片的的方法,可添加文字.图片.视频.音频等.下 ...
- WebRTC从摄像头获取图片传入canvas
WebRTC从摄像头获取图片传入canvas 前面我们已经能够利用WebRTC的功能,通过浏览器打开摄像头,并把预览的图像显示在video元素中. 接下来我们尝试从视频中截取某一帧,显示在界面上. h ...
- electron另一种运行方式
编写helloword 全局安装软件 npm install -g electron 快速编写html html:5 完整代码和流程: 1.index.html <!DOCTYPE htm ...
- 洛谷 P6349 - [PA2011]Kangaroos(KDT+标记下放)
洛谷题面传送门 KDT 上打标记的 hot tea. 考虑将询问 \(A,B\) 看作二维平面直角坐标系上的一个点 \((A,B)\),那么我们这样考虑,我们从左到右扫过全部 \(n\) 个区间并开一 ...