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

@@ -85,13 +85,13 @@ class MyList {
}
//
_size -= 1
//
//
return num
}
/* */
func extendCapacity() {
// extendRatio
// extendRatio
arr = arr + Array(repeating: 0, count: _capacity * (extendRatio - 1))
//
_capacity = arr.count

View File

@@ -13,10 +13,10 @@ func backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]
}
//
for col in 0 ..< n {
// 线线
// 线线
let diag1 = row - col + n - 1
let diag2 = row + col
// 线线
// 线线
if !cols[col] && !diags1[diag1] && !diags2[diag2] {
//
state[row][col] = "Q"
@@ -40,7 +40,7 @@ func nQueens(n: Int) -> [[[String]]] {
var state = Array(repeating: Array(repeating: "#", count: n), count: n)
var cols = Array(repeating: false, count: n) //
var diags1 = Array(repeating: false, count: 2 * n - 1) // 线
var diags2 = Array(repeating: false, count: 2 * n - 1) // 线
var diags2 = Array(repeating: false, count: 2 * n - 1) // 线
var res: [[[String]]] = []
backtrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)

View File

@@ -7,7 +7,7 @@
import graph_adjacency_list_target
import utils
/* 广 BFS */
/* 广 */
// 使便
func graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {
//
@@ -48,7 +48,7 @@ enum GraphBFS {
print("\n初始化后,图为")
graph.print()
/* 广 BFS */
/* 广 */
let res = graphBFS(graph: graph, startVet: v[0])
print("\n广度优先遍历BFS顶点序列为")
print(Vertex.vetsToVals(vets: res))

View File

@@ -7,7 +7,7 @@
import graph_adjacency_list_target
import utils
/* DFS */
/* */
func dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {
res.append(vet) // 访
visited.insert(vet) // 访
@@ -21,7 +21,7 @@ func dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], v
}
}
/* DFS */
/* */
// 使便
func graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {
//
@@ -46,7 +46,7 @@ enum GraphDFS {
print("\n初始化后,图为")
graph.print()
/* DFS */
/* */
let res = graphDFS(graph: graph, startVet: v[0])
print("\n深度优先遍历DFS顶点序列为")
print(Vertex.vetsToVals(vets: res))

View File

@@ -6,7 +6,7 @@
/* */
func maxCapacity(ht: [Int]) -> Int {
// i, j
// i, j使
var i = 0, j = ht.count - 1
// 0
var res = 0

View File

@@ -103,7 +103,7 @@ enum _ArrayHashMap {
map.print()
/* */
// key value
// key value
let name = map.get(key: 15937)!
print("\n输入学号 15937 ,查询到姓名 \(name)")

View File

@@ -24,7 +24,7 @@ enum HashMap {
PrintUtil.printHashMap(map: map)
/* */
// key value
// key value
let name = map[15937]!
print("\n输入学号 15937 ,查询到姓名 \(name)")

View File

@@ -37,13 +37,13 @@ class HashMapChaining {
func get(key: Int) -> String? {
let index = hashFunc(key: key)
let bucket = buckets[index]
// key val
// key val
for pair in bucket {
if pair.key == key {
return pair.val
}
}
// key nil
// key nil
return nil
}
@@ -124,7 +124,7 @@ enum _HashMapChaining {
map.print()
/* */
// key value
// key value
let name = map.get(key: 13276)
print("\n输入学号 13276 ,查询到姓名 \(name!)")

View File

@@ -41,9 +41,9 @@ class HashMapOpenAddressing {
var firstTombstone = -1
// 线
while buckets[index] != nil {
// key
// key
if buckets[index]!.key == key {
//
//
if firstTombstone != -1 {
buckets[firstTombstone] = buckets[index]
buckets[index] = TOMBSTONE
@@ -55,7 +55,7 @@ class HashMapOpenAddressing {
if firstTombstone == -1 && buckets[index] == TOMBSTONE {
firstTombstone = index
}
//
//
index = (index + 1) % capacity
}
// key
@@ -151,7 +151,7 @@ enum _HashMapOpenAddressing {
map.print()
/* */
// key value
// key value
let name = map.get(key: 13276)
print("\n输入学号 13276 ,查询到姓名 \(name!)")

View File

@@ -20,17 +20,17 @@ class MaxHeap {
}
}
/* */
/* */
private func left(i: Int) -> Int {
2 * i + 1
}
/* */
/* */
private func right(i: Int) -> Int {
2 * i + 2
}
/* */
/* */
private func parent(i: Int) -> Int {
(i - 1) / 2 //
}

View File

@@ -34,7 +34,7 @@ func bubbleSortWithFlag(nums: inout [Int]) {
flag = true //
}
}
if !flag { //
if !flag { //
break
}
}

View File

@@ -6,12 +6,12 @@
/* */
func merge(nums: inout [Int], left: Int, mid: Int, right: Int) {
// [left, mid], [mid+1, right]
// [left, mid], [mid+1, right]
// tmp
var tmp = Array(repeating: 0, count: right - left + 1)
//
var i = left, j = mid + 1, k = 0
//
//
while i <= mid, j <= right {
if nums[i] <= nums[j] {
tmp[k] = nums[i]

View File

@@ -45,7 +45,7 @@ func quickSort(nums: inout [Int], left: Int, right: Int) {
/* */
/* */
/* */
func medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int {
if (nums[left] < nums[mid]) != (nums[left] < nums[right]) {
return left

View File

@@ -47,7 +47,7 @@ class ArrayDeque {
return
}
//
// front
// front
front = index(i: front - 1)
// num
nums[front] = num
@@ -60,7 +60,7 @@ class ArrayDeque {
print("双向队列已满")
return
}
// + 1
// + 1
let rear = index(i: front + size())
// num
nums[rear] = num

View File

@@ -36,8 +36,8 @@ class ArrayQueue {
print("队列已满")
return
}
// + 1
// rear
// + 1
// rear
let rear = (front + queSize) % capacity()
// num
nums[rear] = num
@@ -48,7 +48,7 @@ class ArrayQueue {
@discardableResult
func pop() -> Int {
let num = peek()
//
//
front = (front + 1) % capacity()
queSize -= 1
return num

View File

@@ -26,7 +26,7 @@ class LinkedListQueue {
/* */
func push(num: Int) {
// num
// num
let node = ListNode(x: num)
//
if front == nil {

View File

@@ -99,7 +99,7 @@ class AVLTree {
if node == nil {
return TreeNode(x: val)
}
/* 1. */
/* 1. */
if val < node!.val {
node?.left = insertHelper(node: node?.left, val: val)
} else if val > node!.val {
@@ -125,7 +125,7 @@ class AVLTree {
if node == nil {
return nil
}
/* 1. */
/* 1. */
if val < node!.val {
node?.left = removeHelper(node: node?.left, val: val)
} else if val > node!.val {