1 DONE 排序
- State "DONE" from "TODO"
1.1 DONE 两个有序数组合并
1 | func merge(A, B []int) []int { |
1 | package main |
1.2 DONE 插入排序
- State "DONE" from "TODO"
1 | func isort(nums []int) { |
1 | package main |
1.3 DONE 二分排序
1 | func bsort(nums []int) { |
1 | package main |
1.4 DONE 快速排序
1 | func qsort(nums []int) { |
1 | func partition(nums []int) int { |
1 | package main |
1.5 DONE 堆排序
1 | func parent(i int) int { |
1 | package main |
1.6 DONE 计数排序
1 | func countingSort(nums []int) []int { |
1 | package main |
1.7 DONE 基数排序
1 | func radixSort(nums []int) []int { |
1 | package main |
1.8 DONE 桶排序
1 | type ListNode struct { |
1 | package main |
2 DONE 查找
2.1 DONE 二分查找
- State "DONE" from "TODO"
2.1.1 DONE 递归
- State "DONE" from "TODO"
1 | func binarySearch(nums []int, start, end, target int) int { |
2.2 DONE 插值查找
- State "DONE" from "TODO"
1 | func interpolationSearch(nums []int, start, end, target int) int { |
2.3 DONE 斐波那契查找
1 | package main |
2.4 DONE 字符串暴力匹配
1 | func violenMatch(s, p string) bool { |
1 | package main |
2.5 DONE KMP算法
假设现在文本串S匹配到 i 位置,模式串P匹配到 j 位置:
- 如果j = -1,或者当前字符匹配成功(即S[i] == P[j]),都令i++,j++,继续匹配下一个字符
- 如果j != -1,且当前字符匹配失败(即S[i] != P[j]),则令 i 不变,j = next[j]。 此举意味着失配时,模式串P相对于文本串S向右移动了 j-next[j] 位
1 | func getNext(p string) []int { |
1 | package main |
3 DONE 链表
- State "DONE" from "TODO"
3.1 DONE 单向链表
- State "DONE" from "TODO"
1
2
3
4type ListNode struct {
Val int
Next *ListNode
}1
2
3
4
5struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
3.1.1 DONE Rotate List
- State "DONE" from "FIXED"
- State "FIXED" from "TODO"
1 | func rotateRight(head *ListNode, k int) *ListNode { |
1 | package main |
3.1.2 DONE Reverse Linked List
- State "DONE" from "TODO"
1 | func reverseList(head *ListNode) *ListNode { |
3.1.3 DONE Delete Node in a Linked List
- State "DONE" from "TODO"
1 | class Solution { |
3.1.4 DONE Merge Two Sorted Lists
- State "DONE" from "TODO"
1 | func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { |
3.1.5 DONE Linked List Cycle
- State "DONE" from "TODO"
1 | class Solution { |
3.1.6 DONE Linked List Cycle II
- State "DONE" from "TODO"
1 | class Solution { |
3.1.7 DONE 合并K个元素的有序链表
- State "DONE" from "TODO"
1 | import ( |
1 | func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { |
1 | func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { |
3.1.8 DONE 每k个一组翻转链表
- State "DONE" from "TODO"
1 | func reverseKGroup(head *ListNode, k int) *ListNode { |
3.2 DONE 双向链表
- State "DONE" from "TODO"
3.2.1 DONE All O`one Data Structure
- State "DONE" from "TODO"
1 | package main |
4 DONE LIFO 栈
- State "DONE" from "TODO"
4.1 DONE 实现
- State "DONE" from
1 | type Stack struct { |
1 | func main() { |
4.2 DONE 应用
- State "DONE" from "TODO"
4.2.1 DONE Implement Queue using Stacks
- State "DONE" from "TODO"
1 | type MyQueue struct { |
1 | package main |
5 DONE FIFO 队列
5.1 DONE 实现
- State "DONE" from
1 | type Queue struct { |
5.2 DONE 应用
- State "DONE" from "TODO"
5.2.1 DONE Implement Stack using Queues
- State "DONE" from "TODO"
1 | type MyStack struct { |
1 | package main |
6 DONE 二叉堆
6.1 DONE 实现堆
- State "DONE" from "TODO"
1 | type LessFunc func(int, int) bool |
1 | package main |
6.2 DONE 堆的应用
- State "DONE" from "TODO"
6.2.1 DONE Find Median from Data Stream
- State "DONE" from "TODO"
1 | package main |
7 DONE 二项堆
1 | type BinHeapNode struct { |
1 | package main |
8 DONE 斐波那契堆
1 | type FibHeapNode struct { |
1 | package main |
9 WAITING 二叉查找树
- State "WAITING" from "TODO"
等待被测试
1 | type TreeNode struct { |
1 | package main |
10 TODO 红黑树
#+NAME left-leaning-red-black-tree
1 | const ( |
12 DONE 图
- State "DONE" from "TODO"
12.1 DONE 定义
1 | type ArcNode struct { |
1 | package main |
digraph { $input } |
1 | type Edge struct { |
12.2 DONE 广度优先搜索(BFS)
1 | func bfs(vertices AdjList, u int, visitFunc func(interface{})) { |
1 | package main |
12.3 DONE 深度优先搜索(DFS)
- State "DONE" from "TODO"
1 | func dfs(vertices AdjList, visitFunc func(interface{})) { |
1 | package main |
12.4 DONE Union-Find算法
1 | func hasCycle(edges EdgeList, vexNum int) bool { |
1 | package main |
12.5 DONE 拓扑排序
1 | func TopologicalSort(vertices AdjList) ([]int, bool) { |
1 | package main |
1 | func TopologicalSort(vertices AdjList) ([]int, bool) { |
1 | package main |
12.6 DONE 强连通分支
- State "DONE" from "TODO"
12.6.1 DONE Kosaraju算法
- State "DONE" from "TODO"
1 | func reverse(vertices AdjList) AdjList { |
1 | package main |
digraph G { $input } |
12.6.2 DONE Tarjan算法
1 | func tarjan(vertices AdjList) [][]int { |
1 | package main |
digraph G { $input } |
12.6.3 DONE Gabow算法
- State "DONE" from "TODO"
1 | func gabow(vertices AdjList) [][]int { |
1 | package main |
digraph G { $input } |
12.7 DONE 关键路径
1 | func CriticalPath(vertices AdjList) []int { |
1 | package main |
1 | func CriticalPath(vertices AdjList) ([]int, bool) { |
1 | package main |
12.8 DONE 最小生成树
12.8.1 DONE Prim算法
1 | func mstPrim(vertices AdjList, s int) EdgeList { |
1 | package main |
12.8.2 DONE Kruskal算法
1 | func mstKruskal(edges EdgeList, vexNum int) EdgeList { |
1 | package main |
12.9 DONE 最短路径
12.9.1 DONE 单源最短路径
- State "DONE" from "TODO"
- DONE Dijkstra算法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36func dijkstra(vertices AdjList, s int) []int {
visited := make([]bool, len(vertices))
dis := make([]int, len(vertices))
for i, _ := range dis {
dis[i] = math.MaxInt32
}
dis[s] = 0
for {
min := math.MaxInt32
t := -1
for u := 0; u < len(vertices); u += 1 {
if !visited[u] && dis[u] < min {
min = dis[u]
t = u
}
}
if t == -1 {
break
}
visited[t] = true
for j := 0; j < len(vertices[t].ArcNodeArr); j += 1 {
v := vertices[t].ArcNodeArr[j].AdjVex
d := vertices[t].ArcNodeArr[j].Info.(int)
if !visited[v] && dis[t]+d < dis[v] {
dis[v] = dis[t] + d
}
}
}
return dis
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49package main
import "fmt"
import "math"
func main() {
vertices := AdjList{
VNode{
Data: 0,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 1, Info: 10},
ArcNode{AdjVex: 2, Info: 5},
},
},
VNode{
Data: 1,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 2, Info: 2},
ArcNode{AdjVex: 3, Info: 1},
},
},
VNode{
Data: 2,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 1, Info: 3},
ArcNode{AdjVex: 3, Info: 9},
ArcNode{AdjVex: 4, Info: 2},
},
},
VNode{
Data: 3,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 4, Info: 4},
},
},
VNode{
Data: 4,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 0, Info: 7},
ArcNode{AdjVex: 3, Info: 6},
},
},
}
fmt.Println(dijkstra(vertices, 0))
} - DONE Bellman–Ford算法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31func bellmanFord(vertices AdjList, s int) ([]int, bool) {
dist := make([]int, len(vertices))
for i, _ := range dist {
dist[i] = math.MaxInt32
}
dist[s] = 0
for i := 1; i < len(vertices); i += 1 {
for u := 0; u < len(vertices); u += 1 {
for j := 0; j < len(vertices[u].ArcNodeArr); j += 1 {
v := vertices[u].ArcNodeArr[j].AdjVex
d := vertices[u].ArcNodeArr[j].Info.(int)
if dist[u]+d < dist[v] {
dist[v] = dist[u] + d
}
}
}
}
for u := 0; u < len(vertices); u += 1 {
for j := 0; j < len(vertices[u].ArcNodeArr); j += 1 {
v := vertices[u].ArcNodeArr[j].AdjVex
d := vertices[u].ArcNodeArr[j].Info.(int)
if dist[u]+d < dist[v] {
return dist, true
}
}
}
return dist, false
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50package main
import "fmt"
import "math"
func main() {
vertices := AdjList{
VNode{
Data: 0,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 1, Info: 10},
ArcNode{AdjVex: 2, Info: 5},
},
},
VNode{
Data: 1,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 2, Info: 2},
ArcNode{AdjVex: 3, Info: 1},
},
},
VNode{
Data: 2,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 1, Info: 3},
ArcNode{AdjVex: 3, Info: 9},
ArcNode{AdjVex: 4, Info: 2},
},
},
VNode{
Data: 3,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 4, Info: 4},
},
},
VNode{
Data: 4,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 0, Info: 7},
ArcNode{AdjVex: 3, Info: 6},
},
},
}
fmt.Println(bellmanFord(vertices, 0))
}
12.9.2 DONE 每对顶点间的最短路径
- State "DONE" from "TODO"
- DONE Floyd-Warshall算法
- State "DONE" from "TODO"
- DONE 原理描述
- State "DONE" from "TODO"
Floyd-Warshall算法的原理是动态规划。 设 D{i,j,k}为从 i到 j的只以 (1..k)集合中的节点为中间节点的最短路径的长度。
若最短路径经过点k,则 D{i,j,k}=D{i,k,k-1}+D{k,j,k-1}; 若最短路径不经过点k,则 D{i,j,k}=D{i,j,k-1}。 因此, D{i,j,k}=min(D{i,j,k-1},D{i,k,k-1}+D{k,j,k-1})。
在实际算法中,为了节约空间,可以直接在原来空间上进行迭代,这样空间可降至二维。
- DONE 算法实现
- State "DONE" from "TODO"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30func floydWarshall(vertices AdjList) [][]int {
dist := make([][]int, len(vertices))
for i, _ := range dist {
dist[i] = make([]int, len(vertices))
for j, _ := range dist[i] {
dist[i][j] = math.MaxInt32
}
dist[i][i] = 0
}
for u := 0; u < len(vertices); u += 1 {
for j := 0; j < len(vertices[u].ArcNodeArr); j += 1 {
v := vertices[u].ArcNodeArr[j].AdjVex
d := vertices[u].ArcNodeArr[j].Info.(int)
dist[u][v] = d
}
}
for k := 0; k < len(vertices); k += 1 {
for i := 0; i < len(vertices); i += 1 {
for j := 0; j < len(vertices); j += 1 {
if dist[i][j] > dist[i][k]+dist[k][j] {
dist[i][j] = dist[i][k] + dist[k][j]
}
}
}
}
return dist
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49package main
import "fmt"
import "math"
func main() {
vertices := AdjList{
VNode{
Data: 0,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 1, Info: 10},
ArcNode{AdjVex: 2, Info: 5},
},
},
VNode{
Data: 1,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 2, Info: 2},
ArcNode{AdjVex: 3, Info: 1},
},
},
VNode{
Data: 2,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 1, Info: 3},
ArcNode{AdjVex: 3, Info: 9},
ArcNode{AdjVex: 4, Info: 2},
},
},
VNode{
Data: 3,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 4, Info: -4},
},
},
VNode{
Data: 4,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 0, Info: 7},
ArcNode{AdjVex: 3, Info: 6},
},
},
}
fmt.Println(floydWarshall(vertices))
}
- DONE Johnson算法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39func johnson(vertices AdjList) ([][]int, bool) {
v := VNode{
Data: len(vertices),
ArcNodeArr: make([]ArcNode, len(vertices)),
}
for i, _ := range vertices {
v.ArcNodeArr[i] = ArcNode{
AdjVex: i,
Info: 0,
}
}
vertices = append(vertices, v)
hDist, bCycle := bellmanFord(vertices, len(vertices)-1)
if bCycle {
return [][]int{}, bCycle
}
for u := 0; u < len(vertices); u += 1 {
for j := 0; j < len(vertices[u].ArcNodeArr); j += 1 {
v := vertices[u].ArcNodeArr[j].AdjVex
d := vertices[u].ArcNodeArr[j].Info.(int)
d = d + (hDist[u] - hDist[v])
vertices[u].ArcNodeArr[j].Info = d
}
}
vertices = vertices[:len(vertices)-1]
dist := make([][]int, len(vertices))
for i, _ := range vertices {
dist[i] = dijkstra(vertices, i)
for j, _ := range dist[i] {
dist[i][j] = dist[i][j] + (hDist[j] - hDist[i])
}
}
return dist, false
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54package main
import "fmt"
import "math"
func main() {
vertices := AdjList{
VNode{
Data: 0,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 1, Info: 10},
ArcNode{AdjVex: 2, Info: 5},
},
},
VNode{
Data: 1,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 2, Info: 2},
ArcNode{AdjVex: 3, Info: 1},
},
},
VNode{
Data: 2,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 1, Info: 3},
ArcNode{AdjVex: 3, Info: 9},
ArcNode{AdjVex: 4, Info: 2},
},
},
VNode{
Data: 3,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 4, Info: -4},
},
},
VNode{
Data: 4,
ArcNodeArr: []ArcNode{
ArcNode{AdjVex: 0, Info: 7},
ArcNode{AdjVex: 3, Info: 6},
},
},
}
fmt.Println(johnson(vertices))
}
13 DONE Cache
- State "DONE" from "TODO"
13.1 DONE LRU
- State "DONE" from "TODO"
1 | type MyListNode struct { |
1 | type MyListNode struct { |
1 | package main |
13.2 DONE LFU
- State "DONE" from "TODO"
1 | type MyListNode struct { |
Render by hexo-renderer-org with Emacs 25.3.2 (Org mode 8.2.10)