feat: Revised the book (#978)

* Sync recent changes to the revised Word.

* Revised the preface chapter

* Revised the introduction chapter

* Revised the computation complexity chapter

* Revised the chapter data structure

* Revised the chapter array and linked list

* Revised the chapter stack and queue

* Revised the chapter hashing

* Revised the chapter tree

* Revised the chapter heap

* Revised the chapter graph

* Revised the chapter searching

* Reivised the sorting chapter

* Revised the divide and conquer chapter

* Revised the chapter backtacking

* Revised the DP chapter

* Revised the greedy chapter

* Revised the appendix chapter

* Revised the preface chapter doubly

* Revised the figures
This commit is contained in:
Yudong Jin
2023-12-02 06:21:34 +08:00
committed by GitHub
parent b824d149cb
commit e720aa2d24
404 changed files with 1537 additions and 1558 deletions

View File

@ -31,11 +31,11 @@ func insert(nums: inout [Int], num: Int, index: Int) {
for i in nums.indices.dropFirst(index).reversed() {
nums[i] = nums[i - 1]
}
// num index
// num index
nums[index] = num
}
/* index */
/* index */
func remove(nums: inout [Int], index: Int) {
// index
for i in nums.indices.dropFirst(index).dropLast() {

View File

@ -62,7 +62,7 @@ enum LinkedList {
let n2 = ListNode(x: 2)
let n3 = ListNode(x: 5)
let n4 = ListNode(x: 4)
//
//
n0.next = n1
n1.next = n2
n2.next = n3

View File

@ -24,7 +24,7 @@ enum List {
nums.removeAll()
print("清空列表后 nums = \(nums)")
/* */
/* */
nums.append(1)
nums.append(3)
nums.append(2)
@ -32,7 +32,7 @@ enum List {
nums.append(4)
print("添加元素后 nums = \(nums)")
/* */
/* */
nums.insert(6, at: 3)
print("在索引 3 处插入数字 6 ,得到 nums = \(nums)")

View File

@ -4,11 +4,11 @@
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
/* */
class MyList {
private var arr: [Int] //
private var _capacity = 10 //
private var _size = 0 //
private var _size = 0 //
private let extendRatio = 2 //
/* */
@ -16,7 +16,7 @@ class MyList {
arr = Array(repeating: 0, count: _capacity)
}
/* */
/* */
func size() -> Int {
_size
}
@ -43,7 +43,7 @@ class MyList {
arr[index] = num
}
/* */
/* */
func add(num: Int) {
//
if _size == _capacity {
@ -54,7 +54,7 @@ class MyList {
_size += 1
}
/* */
/* */
func insert(index: Int, num: Int) {
if index < 0 || index >= _size {
fatalError("索引越界")
@ -113,7 +113,7 @@ enum _MyList {
static func main() {
/* */
let nums = MyList()
/* */
/* */
nums.add(num: 1)
nums.add(num: 3)
nums.add(num: 2)
@ -121,7 +121,7 @@ enum _MyList {
nums.add(num: 4)
print("列表 nums = \(nums.toArray()) ,容量 = \(nums.capacity()) ,长度 = \(nums.size())")
/* */
/* */
nums.insert(index: 3, num: 6)
print("在索引 3 处插入数字 6 ,得到 nums = \(nums.toArray())")

View File

@ -16,7 +16,7 @@ func backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]
// 线线
let diag1 = row - col + n - 1
let diag2 = row + col
// 线线
// 线线
if !cols[col] && !diags1[diag1] && !diags2[diag2] {
//
state[row][col] = "Q"
@ -39,8 +39,8 @@ func nQueens(n: Int) -> [[[String]]] {
// n*n 'Q' '#'
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 diags1 = 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

@ -30,7 +30,7 @@ func whileLoop(n: Int) -> Int {
func whileLoopII(n: Int) -> Int {
var res = 0
var i = 1 //
// 1, 4, ...
// 1, 4, 10, ...
while i <= n {
res += i
//

View File

@ -12,7 +12,7 @@ func move(src: inout [Int], tar: inout [Int]) {
tar.append(pan)
}
/* f(i) */
/* f(i) */
func dfs(i: Int, src: inout [Int], buf: inout [Int], tar: inout [Int]) {
// src tar
if i == 1 {
@ -27,7 +27,7 @@ func dfs(i: Int, src: inout [Int], buf: inout [Int], tar: inout [Int]) {
dfs(i: i - 1, src: &buf, buf: &src, tar: &tar)
}
/* */
/* */
func solveHanota(A: inout [Int], B: inout [Int], C: inout [Int]) {
let n = A.count
//

View File

@ -22,7 +22,7 @@ func backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {
/* */
func climbingStairsBacktrack(n: Int) -> Int {
let choices = [1, 2] // 1 2
let choices = [1, 2] // 1 2
let state = 0 // 0
var res: [Int] = []
res.append(0) // 使 res[0]

View File

@ -14,7 +14,7 @@ func coinChangeDP(coins: [Int], amt: Int) -> Int {
for a in stride(from: 1, through: amt, by: 1) {
dp[0][a] = MAX
}
//
//
for i in stride(from: 1, through: n, by: 1) {
for a in stride(from: 1, through: amt, by: 1) {
if coins[i - 1] > a {

View File

@ -73,7 +73,7 @@ func editDistanceDP(s: String, t: String) -> Int {
for j in stride(from: 1, through: m, by: 1) {
dp[0][j] = j
}
//
//
for i in stride(from: 1, through: n, by: 1) {
for j in stride(from: 1, through: m, by: 1) {
if s.utf8CString[i - 1] == t.utf8CString[j - 1] {

View File

@ -6,11 +6,11 @@
/* 0-1 */
func knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -> Int {
// 0
// 0
if i == 0 || c == 0 {
return 0
}
//
//
if wgt[i - 1] > c {
return knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)
}
@ -23,7 +23,7 @@ func knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -> Int {
/* 0-1 */
func knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int) -> Int {
// 0
// 0
if i == 0 || c == 0 {
return 0
}
@ -31,7 +31,7 @@ func knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int)
if mem[i][c] != -1 {
return mem[i][c]
}
//
//
if wgt[i - 1] > c {
return knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)
}

View File

@ -58,7 +58,7 @@ func minPathSumDP(grid: [[Int]]) -> Int {
for i in stride(from: 1, to: n, by: 1) {
dp[i][0] = dp[i - 1][0] + grid[i][0]
}
//
//
for i in stride(from: 1, to: n, by: 1) {
for j in stride(from: 1, to: m, by: 1) {
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]

View File

@ -8,7 +8,7 @@ import utils
/* */
public class GraphAdjList {
// key: value
// keyvalue
public private(set) var adjList: [Vertex: [Vertex]]
/* */

View File

@ -67,7 +67,7 @@ class GraphAdjMat {
if i < 0 || j < 0 || i >= size() || j >= size() || i == j {
fatalError("越界")
}
// 沿线 (i, j) == (j, i)
// 线 (i, j) == (j, i)
adjMat[i][j] = 1
adjMat[j][i] = 1
}

View File

@ -23,7 +23,7 @@ func graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {
//
for adjVet in graph.adjList[vet] ?? [] {
if visited.contains(adjVet) {
continue // 访
continue // 访
}
que.append(adjVet) // 访
visited.insert(adjVet) // 访

View File

@ -14,7 +14,7 @@ func dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], v
//
for adjVet in graph.adjList[vet] ?? [] {
if visited.contains(adjVet) {
continue // 访
continue // 访
}
// 访
dfs(graph: graph, visited: &visited, res: &res, vet: adjVet)

View File

@ -6,7 +6,7 @@
import utils
/* */
/* */
class ArrayHashMap {
private var buckets: [Pair?] = []

View File

@ -86,7 +86,7 @@ class MaxHeap {
if isEmpty() {
fatalError("堆为空")
}
//
//
swap(i: 0, j: size() - 1)
//
let val = maxHeap.remove(at: size() - 1)

View File

@ -24,9 +24,9 @@ func binarySearch(nums: [Int], target: Int) -> Int {
return -1
}
/* */
/* */
func binarySearchLCRO(nums: [Int], target: Int) -> Int {
// [0, n) i, j +1
// [0, n) i, j +1
var i = 0
var j = nums.count
// i = j
@ -55,7 +55,7 @@ enum BinarySearch {
var index = binarySearch(nums: nums, target: target)
print("目标元素 6 的索引 = \(index)")
/* */
/* */
index = binarySearchLCRO(nums: nums, target: target)
print("目标元素 6 的索引 = \(index)")
}

View File

@ -6,7 +6,7 @@
/* */
func twoSumBruteForce(nums: [Int], target: Int) -> [Int] {
// O(n^2)
// O(n^2)
for i in nums.indices.dropLast() {
for j in nums.indices.dropFirst(i + 1) {
if nums[i] + nums[j] == target {
@ -19,9 +19,9 @@ func twoSumBruteForce(nums: [Int], target: Int) -> [Int] {
/* */
func twoSumHashTable(nums: [Int], target: Int) -> [Int] {
// O(n)
// O(n)
var dic: [Int: Int] = [:]
// O(n)
// O(n)
for i in nums.indices {
if let j = dic[target - nums[i]] {
return [j, i]

View File

@ -11,7 +11,7 @@ func bucketSort(nums: inout [Double]) {
var buckets = (0 ..< k).map { _ in [Double]() }
// 1.
for num in nums {
// [0, 1)使 num * k [0, k-1]
// [0, 1)使 num * k [0, k-1]
let i = Int(num * Double(k))
// num i
buckets[i].append(num)

View File

@ -37,7 +37,7 @@ func heapSort(nums: inout [Int]) {
}
// n-1
for i in stride(from: nums.count - 1, to: 0, by: -1) {
//
//
nums.swapAt(0, i)
//
siftDown(nums: &nums, n: i, i: 0)

View File

@ -14,7 +14,7 @@ func swap(nums: inout [Int], i: Int, j: Int) {
/* */
/* */
func partition(nums: inout [Int], left: Int, right: Int) -> Int {
// nums[left]
// nums[left]
var i = left
var j = right
while i < j {
@ -87,7 +87,7 @@ func quickSortTailCall(nums: inout [Int], left: Int, right: Int) {
while left < right {
//
let pivot = partition(nums: &nums, left: left, right: right)
//
//
if (pivot - left) < (right - pivot) {
quickSortTailCall(nums: &nums, left: left, right: pivot - 1) //
left = pivot + 1 // [pivot + 1, right]

View File

@ -12,7 +12,7 @@ func digit(num: Int, exp: Int) -> Int {
/* nums k */
func countingSortDigit(nums: inout [Int], exp: Int) {
// 0~9 10
// 0~9 10
var counter = Array(repeating: 0, count: 10)
let n = nums.count
// 0~9

View File

@ -38,7 +38,7 @@ class LinkedListDeque {
/* */
private func push(num: Int, isFront: Bool) {
let node = ListNode(val: num)
// front, rear node
// front rear node
if isEmpty() {
front = node
rear = node

View File

@ -17,7 +17,7 @@ enum BinaryTree {
let n3 = TreeNode(x: 3)
let n4 = TreeNode(x: 4)
let n5 = TreeNode(x: 5)
//
//
n1.left = n2
n1.right = n3
n2.left = n4