解题核心思想:并查集的应用(一种解决方法)
具体题目可以参看leetcode以下中文网址:
leetcode 721网址
本题需要注意的是:具有相同账户名的账户不一定是同一个人拥有的账户,但是合并的多个账户的账户名必定是一样的。
代码如下(java):
import java.util.*;
public class leetcode_0721 {
private int[] father = new int[10000];
public static void main(String[] args) {
List<List<String>> accounts = new ArrayList<>();
List<String> ac1 = new ArrayList<>();
ac1.add("John");
ac1.add("johnsmith@mail.com");
ac1.add("john00@mail.com");
List<String> ac2 = new ArrayList<>();
ac2.add("John");
ac2.add("johnnybravo@mail.com");
List<String> ac3 = new ArrayList<>();
ac3.add("John");
ac3.add("johnsmith@mail.com");
ac3.add("john_newyork@mail.com");
List<String> ac4 = new ArrayList<>();
ac4.add("Mary");
ac4.add("mary@mail.com");
accounts.add(ac1);
accounts.add(ac2);
accounts.add(ac3);
accounts.add(ac4);
List<List<String>> result = new leetcode_0721().accountsMerge(accounts);
for(List<String> account: result) {
System.out.print("[");
for(String data: account) {
System.out.print(data + ", ");
}
System.out.println("]");
}
}
public List<List<String>> accountsMerge(List<List<String>> accounts) {
this.init();
List<List<String>> result = new ArrayList<>();
if(accounts.size() == 0) {
return result;
}
Map<String, String> emailToName = new HashMap<>();
Map<String, Integer> emailToId = new HashMap<>();
Map<Integer, String> idToEmail = new HashMap<>();
int id = 0;
for(List<String> account: accounts) {
String name = "";
String first = account.get(1);
for(String data: account) {
if (name.isEmpty()) {
name = data;
continue;
}
// 该方法当键值对存在时不覆盖键值对,不存在时存入新的键值对
emailToName.putIfAbsent(data, name);
if(!emailToId.containsKey(data)) {
// 查询并去除重复的email
emailToId.put(data, id);
idToEmail.put(id,data);
id ++;
}
this.merge(emailToId.get(first), emailToId.get(data));
}
}
Set<Integer> idKeys = idToEmail.keySet();
// 存储并查集中指向同一个根结点的不同集合
Map<Integer, List<Integer>> idAndSearchSet = new HashMap<>();
for(Integer idKey: idKeys) {
// java8之后。上面的操作可以简化为一行,若key对应的value为空,会将第二个参数的返回值存入并返回
// https://blog.csdn.net/weixin_38229356/article/details/81129320
// 返回存储的键值对的值value
int rootKey = find(idKey);
List<Integer> values = idAndSearchSet.computeIfAbsent(rootKey, key -> new ArrayList<>());
if(rootKey != idKey) {
values.add(idKey);
}
}
Set<Integer> rootKeys = idAndSearchSet.keySet();
for(Integer rootKey: rootKeys) {
String email = idToEmail.get(rootKey);
String name = emailToName.get(email);
List<String> account = new ArrayList<>();
account.add(email);
List<Integer> values = idAndSearchSet.get(rootKey);
for(Integer v: values) {
email = idToEmail.get(v);
account.add(email);
}
// 将account中的email按照字母顺序排序
Collections.sort(account);
// 在account的0位置添加name
account.add(0, name);
result.add(account);
}
return result;
}
// 并查集的初始化
public void init() {
for (int i = 0; i < father.length; i++) {
father[i] = i;
}
}
// 并查集的查询
public int find(int x) {
if(father[x] == x) {
return x;
} else {
return find(father[x]); // 路径的压缩
}
}
// 并查集的合并
public void merge(int x, int y) {
// 将其中一颗树(x)的根节点指向另一颗树(y)的根节点
father[find(x)] = find(y);
}
}
该代码块中的init方法,find的方法和merge方法是并查集中的核心方法,想必对并查集非常熟悉的同学一看就懂。处理该问题的核心代码在accountsMerge方法中。这里主要讲解一下该方法中的核心思想。方法中建立了三个HashMap对象,emailToName对象主要处理email到账户名称name的映射,另外两个HashMap对象emailToId和idToEmail主要处理email到id和id到email的映射,之所以使用这两个对象是因为同一个email可能在多个账户中都会出现,因此使用两个HashMap对象emailToId和IdToEmail来进行一一映射,即唯一的id对应唯一的email。处理一一映射的代码块判定如下。
if(!emailToId.containsKey(data)) {
// 查询并去除重复的email
emailToId.put(data, id);
idToEmail.put(id,data);
id ++;
}
下面这条语句是将id进行并查集集合合并,这是构造并查集的核心语句。
this.merge(emailToId.get(first), emailToId.get(data));
之后是构造并查集根节点id和其下面所有子节点的映射关系,我使用了Map对象来存储不用的集合,其中键是根节点,值是一个List对象存储根节点下面所有的孩子节点(想象一下并查集的树状结构就可以理解了)。
// 存储并查集中指向同一个根结点的不同集合
Map<Integer, List<Integer>> idAndSearchSet = new HashMap<>();
最后根据划分好的各个并查集集合遍历idAndSearchSet,通过idToEmail.get(rootKey)获取email,通过emailToName.get(email)获取name。先将属于同一个集合的所有email存储进List对象account中,排好序再将name存储进List的第0个位置即可。
不足点
上面的代码看leetcode通过结果时间复杂度和空间复杂度都不低,目前能想到的改进的地方是最后处理账户结果的List不用ArrayList而是改成LinkedList,因为需要在List的第0个位置插入元素,链表在链表头插入元素效率是非常高的。