Update the book based on the revised second edition (#1014)

* Revised the book

* Update the book with the second revised edition

* Revise base on the manuscript of the first edition
This commit is contained in:
Yudong Jin
2023-12-28 18:06:09 +08:00
committed by GitHub
parent 19dde675df
commit f68bbb0d59
261 changed files with 643 additions and 647 deletions

View File

@@ -34,7 +34,7 @@ func (l *myList) capacity() int {
/* 访问元素 */
func (l *myList) get(index int) int {
// 索引如果越界则抛出异常,下同
// 索引如果越界则抛出异常,下同
if index < 0 || index >= l.arrSize {
panic("索引越界")
}
@@ -90,13 +90,13 @@ func (l *myList) remove(index int) int {
}
// 更新元素数量
l.arrSize--
// 返回被删除元素
// 返回被删除元素
return num
}
/* 列表扩容 */
func (l *myList) extendCapacity() {
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组拷贝到新数组
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组
l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)
// 更新列表容量
l.arrCapacity = len(l.arr)

View File

@@ -18,10 +18,10 @@ func backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, d
}
// 遍历所有列
for col := 0; col < n; col++ {
// 计算该格子对应的主对角线和对角线
// 计算该格子对应的主对角线和对角线
diag1 := row - col + n - 1
diag2 := row + col
// 剪枝:不允许该格子所在列、主对角线、对角线上存在皇后
// 剪枝:不允许该格子所在列、主对角线、对角线上存在皇后
if !(*cols)[col] && !(*diags1)[diag1] && !(*diags2)[diag2] {
// 尝试:将皇后放置在该格子
(*state)[row][col] = "Q"

View File

@@ -8,7 +8,7 @@ import (
. "github.com/krahets/hello-algo/pkg"
)
/* 广度优先遍历 BFS */
/* 广度优先遍历 */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
func graphBFS(g *graphAdjList, startVet Vertex) []Vertex {
// 顶点遍历序列

View File

@@ -22,7 +22,7 @@ func TestGraphBFS(t *testing.T) {
fmt.Println("初始化后,图为:")
graph.print()
/* 广度优先遍历 BFS */
/* 广度优先遍历 */
res := graphBFS(graph, vets[0])
fmt.Println("广度优先遍历BFS顶点序列为:")
PrintSlice(VetsToVals(res))

View File

@@ -8,7 +8,7 @@ import (
. "github.com/krahets/hello-algo/pkg"
)
/* 深度优先遍历 DFS 辅助函数 */
/* 深度优先遍历辅助函数 */
func dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {
// append 操作会返回新的的引用必须让原引用重新赋值为新slice的引用
*res = append(*res, vet)
@@ -23,7 +23,7 @@ func dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex
}
}
/* 深度优先遍历 DFS */
/* 深度优先遍历 */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
func graphDFS(g *graphAdjList, startVet Vertex) []Vertex {
// 顶点遍历序列

View File

@@ -21,7 +21,7 @@ func TestGraphDFS(t *testing.T) {
fmt.Println("初始化后,图为:")
graph.print()
/* 深度优先遍历 DFS */
/* 深度优先遍历 */
res := graphDFS(graph, vets[0])
fmt.Println("深度优先遍历DFS顶点序列为:")
PrintSlice(VetsToVals(res))

View File

@@ -8,7 +8,7 @@ import "math"
/* 最大容量:贪心 */
func maxCapacity(ht []int) int {
// 初始化 i, j 分列数组两端
// 初始化 i, j,使其分列数组两端
i, j := 0, len(ht)-1
// 初始最大容量为 0
res := 0

View File

@@ -24,7 +24,7 @@ func TestArrayHashMap(t *testing.T) {
hmap.print()
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
// 向哈希表输入键 key ,得到值 value
name := hmap.get(15937)
fmt.Println("\n输入学号 15937 ,查询到姓名 " + name)

View File

@@ -24,7 +24,7 @@ func TestHashMapChaining(t *testing.T) {
hmap.print()
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
// 向哈希表输入键 key ,得到值 value
name := hmap.get(15937)
fmt.Println("\n输入学号 15937 ,查询到姓名 ", name)
@@ -50,7 +50,7 @@ func TestHashMapOpenAddressing(t *testing.T) {
hmap.print()
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
// 向哈希表输入键 key ,得到值 value
name := hmap.get(13276)
fmt.Println("\n输入学号 13276 ,查询到姓名 ", name)

View File

@@ -48,13 +48,13 @@ func (m *hashMapChaining) loadFactor() float64 {
func (m *hashMapChaining) get(key int) string {
idx := m.hashFunc(key)
bucket := m.buckets[idx]
// 遍历桶,若找到 key 则返回对应 val
// 遍历桶,若找到 key 则返回对应 val
for _, p := range bucket {
if p.key == key {
return p.val
}
}
// 若未找到 key 则返回空字符串
// 若未找到 key 则返回空字符串
return ""
}

View File

@@ -50,7 +50,7 @@ func (m *hashMapOpenAddressing) get(key int) string {
idx := m.hashFunc(key)
// 线性探测,从 index 开始向后遍历
for i := 0; i < m.capacity; i++ {
// 计算桶索引,越过尾部返回头部
// 计算桶索引,越过尾部返回头部
j := (idx + i) % m.capacity
// 若遇到空桶,说明无此 key ,则返回 null
if m.buckets[j] == (pair{}) {
@@ -61,7 +61,7 @@ func (m *hashMapOpenAddressing) get(key int) string {
return m.buckets[j].val
}
}
// 若未找到 key 则返回空字符串
// 若未找到 key 则返回空字符串
return ""
}
@@ -74,7 +74,7 @@ func (m *hashMapOpenAddressing) put(key int, val string) {
idx := m.hashFunc(key)
// 线性探测,从 index 开始向后遍历
for i := 0; i < m.capacity; i++ {
// 计算桶索引,越过尾部返回头部
// 计算桶索引,越过尾部返回头部
j := (idx + i) % m.capacity
// 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶
if m.buckets[j] == (pair{}) || m.buckets[j] == m.removed {
@@ -99,7 +99,7 @@ func (m *hashMapOpenAddressing) remove(key int) {
// 遍历桶,从中删除键值对
// 线性探测,从 index 开始向后遍历
for i := 0; i < m.capacity; i++ {
// 计算桶索引,越过尾部返回头部
// 计算桶索引,越过尾部返回头部
j := (idx + i) % m.capacity
// 若遇到空桶,说明无此 key ,则直接返回
if m.buckets[j] == (pair{}) {

View File

@@ -27,7 +27,7 @@ func TestHashMap(t *testing.T) {
PrintMap(hmap)
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
// 向哈希表输入键 key ,得到值 value
name := hmap[15937]
fmt.Println("\n输入学号 15937 ,查询到姓名 ", name)

View File

@@ -33,17 +33,17 @@ func newMaxHeap(nums []any) *maxHeap {
return h
}
/* 获取左子节点索引 */
/* 获取左子节点索引 */
func (h *maxHeap) left(i int) int {
return 2*i + 1
}
/* 获取右子节点索引 */
/* 获取右子节点索引 */
func (h *maxHeap) right(i int) int {
return 2*i + 2
}
/* 获取父节点索引 */
/* 获取父节点索引 */
func (h *maxHeap) parent(i int) int {
// 向下整除
return (i - 1) / 2

View File

@@ -31,7 +31,7 @@ func bubbleSortWithFlag(nums []int) {
flag = true // 记录交换元素
}
}
if flag == false { // 此轮冒泡未交换任何元素,直接跳出
if flag == false { // 此轮冒泡未交换任何元素,直接跳出
break
}
}

View File

@@ -6,12 +6,12 @@ package chapter_sorting
/* 合并左子数组和右子数组 */
func merge(nums []int, left, mid, right int) {
// 左子数组区间 [left, mid], 右子数组区间 [mid+1, right]
// 左子数组区间 [left, mid], 右子数组区间 [mid+1, right]
// 创建一个临时数组 tmp ,用于存放合并后的结果
tmp := make([]int, right-left+1)
// 初始化左子数组和右子数组的起始索引
i, j, k := left, mid+1, 0
// 当左右子数组都还有元素时,比较并将较小的元素复制到临时数组中
// 当左右子数组都还有元素时,进行比较并将较小的元素复制到临时数组中
for i <= mid && j <= right {
if nums[i] <= nums[j] {
tmp[k] = nums[i]

View File

@@ -45,7 +45,7 @@ func (q *quickSort) quickSort(nums []int, left, right int) {
q.quickSort(nums, pivot+1, right)
}
/* 选取三个元素的中位数 */
/* 选取三个候选元素的中位数 */
func (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int {
// 此处使用异或运算来简化代码(!= 在这里起到异或的作用)
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1

View File

@@ -49,7 +49,7 @@ func (q *arrayDeque) pushFirst(num int) {
return
}
// 队首指针向左移动一位
// 通过取余操作实现 front 越过数组头部后回到尾部
// 通过取余操作实现 front 越过数组头部后回到尾部
q.front = q.index(q.front - 1)
// 将 num 添加至队首
q.nums[q.front] = num
@@ -62,7 +62,7 @@ func (q *arrayDeque) pushLast(num int) {
fmt.Println("双向队列已满")
return
}
// 计算尾指针,指向队尾索引 + 1
// 计算尾指针,指向队尾索引 + 1
rear := q.index(q.front + q.queSize)
// 将 num 添加至队首
q.nums[rear] = num

View File

@@ -38,8 +38,8 @@ func (q *arrayQueue) push(num int) {
if q.queSize == q.queCapacity {
return
}
// 计算尾指针,指向队尾索引 + 1
// 通过取余操作实现 rear 越过数组尾部后回到头部
// 计算尾指针,指向队尾索引 + 1
// 通过取余操作实现 rear 越过数组尾部后回到头部
rear := (q.front + q.queSize) % q.queCapacity
// 将 num 添加至队尾
q.nums[rear] = num
@@ -49,7 +49,7 @@ func (q *arrayQueue) push(num int) {
/* 出队 */
func (q *arrayQueue) pop() any {
num := q.peek()
// 队首指针向后移动一位,若越过尾部则返回到数组头部
// 队首指针向后移动一位,若越过尾部则返回到数组头部
q.front = (q.front + 1) % q.queCapacity
q.queSize--
return num

View File

@@ -116,7 +116,7 @@ func (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {
if node == nil {
return NewTreeNode(val)
}
/* 1. 查找插入位置并插入节点 */
/* 1. 查找插入位置并插入节点 */
if val < node.Val.(int) {
node.Left = t.insertHelper(node.Left, val)
} else if val > node.Val.(int) {
@@ -143,7 +143,7 @@ func (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {
if node == nil {
return nil
}
/* 1. 查找节点并删除 */
/* 1. 查找节点并删除 */
if val < node.Val.(int) {
node.Left = t.removeHelper(node.Left, val)
} else if val > node.Val.(int) {