给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails 表示该账户的邮箱地址。
现在,我们想合并这些账户。如果两个账户都有一些共同的邮箱地址,则两个账户必定属于同一个人。请注意,即使两个账户具有相同的名称,它们也可能属于不同的人,因为人们可能具有相同的名称。一个人最初可以拥有任意数量的账户,但其所有账户都具有相同的名称。
合并账户后,按以下格式返回账户:每个账户的第一个元素是名称,其余元素是按字符 ASCII 顺序排列的邮箱地址。账户本身可以以任意顺序返回。
示例一
输入:
accounts = [[“John”, “johnsmith@mail.com”, “john00@mail.com”], [“John”, “johnnybravo@mail.com”], [“John”, “johnsmith@mail.com”, “john_newyork@mail.com”], [“Mary”, “mary@mail.com”]]
输出:
[[“John”, ‘john00@mail.com’, ‘john_newyork@mail.com’, ‘johnsmith@mail.com’], [“John”, “johnnybravo@mail.com”], [“Mary”, “mary@mail.com”]]
解释:
第一个和第三个 John 是同一个人,因为他们有共同的邮箱地址 “johnsmith@mail.com”。
第二个 John 和 Mary 是不同的人,因为他们的邮箱地址没有被其他帐户使用。
可以以任何顺序返回这些列表,例如答案 [[‘Mary’,‘mary@mail.com’],[‘John’,‘johnnybravo@mail.com’],
[‘John’,‘john00@mail.com’,‘john_newyork@mail.com’,‘johnsmith@mail.com’]] 也是正确的。
提示:
- accounts的长度将在[1,1000]的范围内。
- accounts[i]的长度将在[1,10]的范围内。
- accounts[i][j]的长度将在[1,30]的范围内。
根据题意可知:
- 存在相同邮箱的账号一定属于同一个人
- 名称相同的账户不一定属于同一个人
- 先初始化每个账户为1个连通分量
- 遍历每个账户下的邮箱,判断该邮箱是否在其他账户下出现
- 如果未出现,继续
- 如果账户A、B下出现了相同的邮箱email,那么将账户A和账户B两个连
- 分量进行合并
- 最后遍历并查集中每个连通分量,将所有连通分量内部账户的邮箱全部
并(相同的去重,不同的合并)
并查集
思路:以accounts的索引构造并查集,
- 先初始化每个账户为1个连通分量。
- 遍历每个账户下的邮箱,判断该邮箱是否在其他账户下出现,如果账户A、B下出现了相同的邮箱email,那么将账户A和账户B两个连分量进行合并;否则,继续遍历;
- 最后遍历并查集中每个连通分量,将所有连通分量内部账户的邮箱全部
合并(相同的去重,不同的合并)
class Solution {
public:
//记录节点的根
vector<int> parent;
//节点路径深度,用于压缩路径
vector<int> rank;
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
int n = accounts.size();
parent.resize(n, 0);
rank.resize(n, 0);
//初始化parent和rank
initParent(n);
//存储结果集
vector<vector<string>> res;
//映射,{email : id},用于保存每个email所在的账户id
unordered_map<string, int> map;
for(int i=0; i<n; i++){
for(int j=1; j<accounts[i].size(); j++){
string email = accounts[i][j];
//如果map中不存在当前此email,就保存
if(!map.count(email)){
map[email] = i;
}
//否则,合并
else{
merge(map[email], i);
}
}
}
{id : 所有的email}
unordered_map<int, vector<string>> master;
//遍历map,将属于同一连通分量的email保存到一起
for(auto& [k, v] : map){
master[find(v)].push_back(k);
}
for(auto& [k, v] : master){
vector<string> ans;
//先将账户人加入
ans.push_back(accounts[k][0]);
//排序
sort(v.begin(), v.end());
//将v的所有值插入到ans后
ans.insert(ans.end(), v.begin(), v.end());
res.push_back(ans);
}
return res;
}
void initParent(int n){
for(int i=0; i<n; i++){
parent[i] = i;
}
}
//查找,parent[i]的值是其父节点
int find(int x){
int root = x;
while(parent[root] != root){
root = parent[root];
}
return root;
}
//合并连通分量
void merge(int x, int y){
int xRoot = find(x);
int yRoot = find(y);
if(xRoot == yRoot){
return;
}
if(rank[xRoot] > rank[yRoot]){
parent[yRoot] = xRoot;
}
else if(rank[xRoot] < rank[yRoot]){
parent[xRoot] = yRoot;
}
else{
parent[xRoot] = yRoot;
rank[yRoot]++;
}
}
};
图dfs
思路:
- 首先构建无向图图的邻接表,遍历每个账户,以此账户accounts[i]的第一个email,即accounts[i][1]为起点,将其后面的邮箱加入到email的邻接表中,注意,因为无向图,所以也要将email加入到其后面的邮箱的邻接表
- 深度优先搜索邻接表,如果访问过的点,跳过
- 将未访问过的点加入到数组ans中
- 遍历结束,将ans加入到结果集res中
class Solution {
public:
//邻接表
unordered_map<string, vector<string>> graph;
//用于判断email是否访问过
unordered_set<string> visited;
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
int n = accounts.size();
vector<vector<string>> res;
//构建邻接矩阵
for(auto& account : accounts){
//以第一个地址为起点
string email = account[1];
for(int j=2; j<account.size(); j++){
graph[email].push_back(account[j]);
graph[account[j]].push_back(email);
}
}
for(auto& account : accounts){
vector<string> ans;
dfs(account[1], ans);
//可能存在为空的情况,跳过
if(ans.empty()) continue;
sort(ans.begin(), ans.end());
ans.insert(ans.begin(), account[0]);
res.push_back(ans);
}
return res;
}
//由于传参需要拷贝,耗时耗存,所以这里的参数以引用的形式赋予地址
void dfs(string& email, vector<string>& ans){
//如果已经访问过,剪枝
if(visited.count(email)){
return;
}
visited.emplace(email);
//保存此图的eamil
ans.push_back(email);
for(string& s : graph[email]){
dfs(s, ans);
}
}
};
答案整理,来源于题解和评论区。