-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a77d1c
commit 89f9edd
Showing
8 changed files
with
127 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
class Solution { | ||
public List<List<String>> accountsMerge(List<List<String>> accounts) { | ||
//构建邮件索引和名称映射 | ||
Map<String, Integer> emailToIndex = new HashMap<String, Integer>(); | ||
Map<String, String> emailToName = new HashMap<String, String>(); | ||
int emailsCount = 0; | ||
for (List<String> account : accounts) { | ||
String name = account.get(0); | ||
int size = account.size(); | ||
for (int i = 1; i < size; i++) { | ||
String email = account.get(i); | ||
if (!emailToIndex.containsKey(email)) { | ||
emailToIndex.put(email, emailsCount++); | ||
emailToName.put(email, name); | ||
} | ||
} | ||
} | ||
//设计并查集 连接同一个账户的不同邮件 | ||
UnionFind uf = new UnionFind(emailsCount); | ||
for (List<String> account : accounts) { | ||
String firstEmail = account.get(1); | ||
int firstIndex = emailToIndex.get(firstEmail); | ||
int size = account.size(); | ||
for (int i = 2; i < size; i++) { | ||
String nextEmail = account.get(i); | ||
int nextIndex = emailToIndex.get(nextEmail); | ||
uf.union(firstIndex, nextIndex); | ||
} | ||
} | ||
//连接相关联的邮件 | ||
Map<Integer, List<String>> indexToEmails = new HashMap<Integer, List<String>>(); | ||
for (String email : emailToIndex.keySet()) { | ||
int index = uf.find(emailToIndex.get(email)); | ||
List<String> account = indexToEmails.getOrDefault(index, new ArrayList<String>()); | ||
account.add(email); | ||
indexToEmails.put(index, account); | ||
} | ||
List<List<String>> merged = new ArrayList<List<String>>(); | ||
for (List<String> emails : indexToEmails.values()) { | ||
Collections.sort(emails); | ||
String name = emailToName.get(emails.get(0)); | ||
List<String> account = new ArrayList<String>(); | ||
account.add(name); | ||
account.addAll(emails); | ||
merged.add(account); | ||
} | ||
return merged; | ||
} | ||
} | ||
|
||
class UnionFind { | ||
int[] parent; | ||
|
||
public UnionFind(int n) { | ||
parent = new int[n]; | ||
for (int i = 0; i < n; i++) { | ||
parent[i] = i; | ||
} | ||
} | ||
|
||
public void union(int index1, int index2) { | ||
parent[find(index2)] = find(index1); | ||
} | ||
|
||
public int find(int index) { | ||
if (parent[index] != index) { | ||
parent[index] = find(parent[index]); | ||
} | ||
return parent[index]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# 并查集 Disjoint Set Union dsu | ||
## 1. 并查集是什么 | ||
特殊的树形结构 森林 | ||
并查集是一种数据结构,用于处理一些不相交集合的合并及查询问题。在一些有N个元素的集合应用问题中, | ||
我们通常是在开始时让每个元素构成一个单元素的集合, | ||
然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,6 @@ | |
|
||
|
||
|
||
## tree | ||
|
||
- [并查集](/dataStructure/tree/并查集.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
[ | ||
"docs/java/framework/springboot/freemarker.md", | ||
"docs/java/framework/springboot/sqlite.md", | ||
"docs/outline/javaFramework.md", | ||
"docs/outline/java_related_website.md", | ||
"docs/outline/navigation/nav.md", | ||
"docs/outline/dataStructure.md", | ||
"docs/python/basic/object_oriented.md", | ||
"README.md", | ||
"docs/index.md", | ||
"docs/api-examples.md", | ||
"docs/markdown-examples.md" | ||
"docs/outline/navigation/nav.md" | ||
] |
Empty file.