Skip to content

Commit

Permalink
edit link
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspercliff committed Jul 15, 2024
1 parent 4a77d1c commit 89f9edd
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 7 deletions.
20 changes: 20 additions & 0 deletions .idea/leetcode/editor.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/leetcode/statistics.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions docs/.vitepress/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export default defineConfig({
description: "jasper learn note",
head: [["link", { rel: "icon", href: "/favicon.ico" }]],
themeConfig: {
editLink: {
pattern: 'https://github.com/jaspercliff/jaspernote/edit/master/docs/:path'
},
nav,
sidebar,
socialLinks,
Expand Down
71 changes: 71 additions & 0 deletions docs/dataStructure/tree/assets/EmailAccountMerger.java
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];
}
}
7 changes: 7 additions & 0 deletions docs/dataStructure/tree/并查集.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 并查集 Disjoint Set Union dsu
## 1. 并查集是什么
特殊的树形结构 森林
并查集是一种数据结构,用于处理一些不相交集合的合并及查询问题。在一些有N个元素的集合应用问题中,
我们通常是在开始时让每个元素构成一个单元素的集合,
然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中。

3 changes: 3 additions & 0 deletions docs/outline/dataStructure.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@



## tree

- [并查集](/dataStructure/tree/并查集.md)
10 changes: 3 additions & 7 deletions docs/public/latestMdFiles.json
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.

0 comments on commit 89f9edd

Please sign in to comment.