Skip to content

Commit 9b64dac

Browse files
committed
字典树Trie和并查集
1 parent c980520 commit 9b64dac

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed

src/数据结构/图/冗余连接.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,44 @@
44
* Created by 周杰伦 on 2018/4/13.
55
*/
66
public class 冗余连接 {
7+
public int[] findRedundantConnection(int[][] edges) {
8+
int N = edges.length;
9+
UnionFind uf = new UnionFind(N);
10+
for (int[] i : edges) {
11+
int u = i[0];
12+
int v = i[1];
13+
if (uf.find(u) == uf.find(v)) {
14+
return i;
15+
}
16+
uf.join(u, v);
17+
}
18+
return new int[]{-1,-1};
19+
}
20+
class UnionFind {
21+
22+
UnionFind (int N) {
23+
//一条边就有两个点,所以节点数是边数加一
24+
pre = new int[N + 1];
25+
for (int i = 0;i < pre.length;i ++) {
26+
pre[i] = i;
27+
}
28+
}
29+
int []pre;
30+
int find(int u) {
31+
return pre[u];
32+
}
33+
34+
void join (int u,int v) {
35+
int preU = find(u);
36+
int preV = find(v);
37+
if (preU == preV)return;
38+
//把所有父节点为U的节点指向V节点。因为题目要求把一个图变为一个树
39+
//所以只能有一个根节点,必须把所有二级节点都指向唯一一个节点。
40+
for (int i = 0; i < pre.length; i++) {
41+
if (pre[i] == preU) {
42+
pre[i] = preV;
43+
}
44+
}
45+
}
46+
}
747
}

src/数据结构/图/并查集.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package 数据结构.;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/26.
5+
*/
6+
public class 并查集 {
7+
//并查集用来查找一个节点的父节点是谁。
8+
//如果两个节点的父节点不同并且互相不为父节点,那么他们是不连通的两个点。
9+
//如果两个连通图要合并,只要让一个图的最高级节点指向另一个图的最高级节点
10+
//通过这个方法可以判断使图中节点连捅需要多少的边。
11+
class UnionFind {
12+
13+
UnionFind (int []edges) {
14+
pre = new int[edges.length];
15+
for (int i = 0;i < pre.length;i ++) {
16+
pre[i] = i;
17+
}
18+
}
19+
int []pre;
20+
int find(int u) {
21+
return pre[u];
22+
}
23+
24+
void join (int u,int v) {
25+
int preU = find(u);
26+
int preV = find(v);
27+
if (preU == preV)return;
28+
pre[preU] = preV;
29+
return;
30+
}
31+
}
32+
}

src/数据结构/树/BST/从有序数组中构造二叉查找树.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
public class 从有序数组中构造二叉查找树 {
99
// Leetcode : 108. Convert Sorted Array to Binary Search Tree (Easy)
1010

11+
//很有难度啊,必须知道二叉查找树可以用二分法来构造子树,每个中点都是一个根节点。
1112
public TreeNode sortedArrayToBST(int[] nums) {
1213
return toBST(nums, 0, nums.length - 1);
1314
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package 数据结构..Trie;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/26.
5+
*/
6+
public class Trie求前缀和 {
7+
class MapSum {
8+
9+
private class Node {
10+
Node[] child = new Node[26];
11+
int value;
12+
}
13+
14+
private Node root = new Node();
15+
16+
public MapSum() {
17+
18+
}
19+
//插入到最后设置节点值
20+
public void insert(String key, int val) {
21+
insert(key, root, val);
22+
}
23+
24+
private void insert(String key, Node node, int val) {
25+
if (node == null) return;
26+
if (key.length() == 0) {
27+
node.value = val;
28+
return;
29+
}
30+
int index = indexForChar(key.charAt(0));
31+
if (node.child[index] == null) {
32+
node.child[index] = new Node();
33+
}
34+
insert(key.substring(1), node.child[index], val);
35+
}
36+
37+
public int sum(String prefix) {
38+
return sum(prefix, root);
39+
}
40+
41+
private int sum(String prefix, Node node) {
42+
if (node == null) return 0;
43+
if (prefix.length() != 0) {
44+
//先进行前缀匹配,匹配到最后一个节点。
45+
int index = indexForChar(prefix.charAt(0));
46+
return sum(prefix.substring(1), node.child[index]);
47+
}
48+
//从匹配到的最后一个节点开始,查找所有子节点,加上这些子节点的和。
49+
//并且递归地加上这些子节点往下的节点值。
50+
int sum = node.value;
51+
for (Node child : node.child) {
52+
sum += sum(prefix, child);
53+
}
54+
return sum;
55+
}
56+
57+
private int indexForChar(char c) {
58+
return c - 'a';
59+
}
60+
}
61+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package 数据结构..Trie;
2+
3+
/**
4+
* Created by 周杰伦 on 2018/4/26.
5+
*/
6+
public class 实现一个Trie树 {
7+
class Trie {
8+
Node root = new Node();
9+
class Node {
10+
Node[] childs = new Node[26];
11+
boolean isLeaf;
12+
}
13+
/** Initialize your data structure here. */
14+
public Trie() {
15+
16+
}
17+
18+
/** Inserts a word into the trie. */
19+
public void insert(String word) {
20+
insert(word, root);
21+
}
22+
public void insert(String word, Node node) {
23+
if (word.length() == 0 ) {
24+
node.isLeaf = true;
25+
return;
26+
}
27+
int index = indexForChar(word.charAt(0));
28+
if (node.childs[index] == null) {
29+
node.childs[index] = new Node();
30+
}
31+
insert(word.substring(1), node.childs[index]);
32+
}
33+
/** Returns if the word is in the trie. */
34+
public boolean search(String word) {
35+
return search(word, root);
36+
}
37+
public boolean search(String word,Node node) {
38+
if (node == null) return false;
39+
if (word.length() == 0 ) return node.isLeaf;
40+
int index = indexForChar(word.charAt(0));
41+
return search(word.substring(1), node.childs[index]);
42+
}
43+
/** Returns if there is any word in the trie that starts with the given prefix. */
44+
public boolean startsWith(String prefix) {
45+
return startsWith(prefix, root);
46+
}
47+
public boolean startsWith(String prefix, Node node) {
48+
if (node == null) return false;
49+
if (prefix.length() == 0 ) return true;
50+
int index = indexForChar(prefix.charAt(0));
51+
return startsWith(prefix.substring(1), node.childs[index]);
52+
}
53+
54+
public int indexForChar(char c) {
55+
return c - 'a';
56+
}
57+
}
58+
59+
}

0 commit comments

Comments
 (0)