From d8caf02e9e06f2c8ecb8d7efea6b0152c1a49ef9 Mon Sep 17 00:00:00 2001 From: krahets Date: Sun, 7 Apr 2024 03:05:15 +0800 Subject: [PATCH] build --- docs/chapter_array_and_linkedlist/array.md | 9 ++--- .../linked_list.md | 15 +++++--- docs/chapter_array_and_linkedlist/list.md | 12 +++---- .../backtracking_algorithm.md | 18 +++++----- docs/chapter_backtracking/n_queens_problem.md | 16 ++++----- .../permutations_problem.md | 18 +++++----- .../subset_sum_problem.md | 34 +++++++++---------- .../space_complexity.md | 4 +-- .../time_complexity.md | 21 ++++++------ .../basic_data_types.md | 5 +-- .../chapter_data_structure/number_encoding.md | 2 +- docs/chapter_hashing/hash_collision.md | 3 ++ docs/chapter_stack_and_queue/deque.md | 4 +-- en/docs/chapter_array_and_linkedlist/array.md | 9 ++--- .../linked_list.md | 15 +++++--- en/docs/chapter_array_and_linkedlist/list.md | 12 +++---- .../space_complexity.md | 4 +-- .../time_complexity.md | 9 +++-- .../chapter_data_structure/number_encoding.md | 2 +- en/docs/chapter_hashing/hash_collision.md | 3 ++ en/docs/chapter_stack_and_queue/deque.md | 4 +-- 21 files changed, 118 insertions(+), 101 deletions(-) diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index cb7035b04..bdfd6a4a5 100755 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -709,8 +709,8 @@ comments: true ### 删除索引 index 处的元素 ### def remove(nums, index) # 把索引 index 之后的所有元素向前移动一位 - for i in index...nums.length - nums[i] = nums[i + 1] || 0 + for i in index...(nums.length - 1) + nums[i] = nums[i + 1] end end ``` @@ -949,7 +949,7 @@ comments: true count += nums[i] } // 直接遍历数组元素 - for (j: Int in nums) { + for (j in nums) { count += j } } @@ -1155,7 +1155,8 @@ comments: true /* 在数组中查找指定元素 */ fun find(nums: IntArray, target: Int): Int { for (i in nums.indices) { - if (nums[i] == target) return i + if (nums[i] == target) + return i } return -1 } diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 839cf9114..df33f9097 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -597,7 +597,7 @@ comments: true === "Kotlin" ```kotlin title="linked_list.kt" - /* 在链表的节点 n0 之后插入节点p */ + /* 在链表的节点 n0 之后插入节点 P */ fun insert(n0: ListNode?, p: ListNode?) { val n1 = n0?.next p?.next = n1 @@ -811,9 +811,11 @@ comments: true ```kotlin title="linked_list.kt" /* 删除链表的节点 n0 之后的首个节点 */ fun remove(n0: ListNode?) { - val p = n0?.next + if (n0?.next == null) + return + val p = n0.next val n1 = p?.next - n0?.next = n1 + n0.next = n1 } ``` @@ -1018,7 +1020,9 @@ comments: true fun access(head: ListNode?, index: Int): ListNode? { var h = head for (i in 0..= size) - throw IndexOutOfBoundsException() + throw IndexOutOfBoundsException("索引越界") return arr[index] } @@ -2244,7 +2244,7 @@ comments: true fun remove(index: Int): Int { if (index < 0 || index >= size) throw IndexOutOfBoundsException("索引越界") - val num: Int = arr[index] + val num = arr[index] // 将将索引 index 之后的元素都向前移动一位 for (j in index..): Boolean { + fun isSolution(state: MutableList): Boolean { return state.isNotEmpty() && state[state.size - 1]?.value == 7 } /* 记录解 */ - fun recordSolution(state: MutableList?, res: MutableList?>) { - res.add(state?.let { ArrayList(it) }) + fun recordSolution(state: MutableList?, res: MutableList?>) { + res.add(state!!.toMutableList()) } /* 判断在当前状态下,该选择是否合法 */ - fun isValid(state: List?, choice: TreeNode?): Boolean { + fun isValid(state: MutableList?, choice: TreeNode?): Boolean { return choice != null && choice.value != 3 } @@ -1817,8 +1817,8 @@ comments: true /* 回溯算法:例题三 */ fun backtrack( state: MutableList, - choices: List, - res: MutableList?> + choices: MutableList, + res: MutableList?> ) { // 检查是否为解 if (isSolution(state)) { @@ -1832,7 +1832,7 @@ comments: true // 尝试:做出选择,更新状态 makeChoice(state, choice) // 进行下一轮选择 - backtrack(state, listOf(choice!!.left, choice.right), res) + backtrack(state, mutableListOf(choice!!.left, choice.right), res) // 回退:撤销选择,恢复到之前的状态 undoChoice(state, choice) } diff --git a/docs/chapter_backtracking/n_queens_problem.md b/docs/chapter_backtracking/n_queens_problem.md index 6539e03ff..b96ece247 100644 --- a/docs/chapter_backtracking/n_queens_problem.md +++ b/docs/chapter_backtracking/n_queens_problem.md @@ -646,17 +646,17 @@ comments: true fun backtrack( row: Int, n: Int, - state: List>, - res: MutableList>?>, + state: MutableList>, + res: MutableList>?>, cols: BooleanArray, diags1: BooleanArray, diags2: BooleanArray ) { // 当放置完所有行时,记录解 if (row == n) { - val copyState: MutableList> = ArrayList() + val copyState = mutableListOf>() for (sRow in state) { - copyState.add(ArrayList(sRow)) + copyState.add(sRow.toMutableList()) } res.add(copyState) return @@ -685,11 +685,11 @@ comments: true } /* 求解 n 皇后 */ - fun nQueens(n: Int): List>?> { + fun nQueens(n: Int): MutableList>?> { // 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位 - val state: MutableList> = ArrayList() + val state = mutableListOf>() for (i in 0.. = ArrayList() + val row = mutableListOf() for (j in 0..>?> = ArrayList() + val res = mutableListOf>?>() backtrack(0, n, state, res, cols, diags1, diags2) diff --git a/docs/chapter_backtracking/permutations_problem.md b/docs/chapter_backtracking/permutations_problem.md index 8653a1482..a67739b7b 100644 --- a/docs/chapter_backtracking/permutations_problem.md +++ b/docs/chapter_backtracking/permutations_problem.md @@ -471,11 +471,11 @@ comments: true state: MutableList, choices: IntArray, selected: BooleanArray, - res: MutableList?> + res: MutableList?> ) { // 当状态长度等于元素数量时,记录解 if (state.size == choices.size) { - res.add(ArrayList(state)) + res.add(state.toMutableList()) return } // 遍历所有选择 @@ -496,9 +496,9 @@ comments: true } /* 全排列 I */ - fun permutationsI(nums: IntArray): List?> { - val res: MutableList?> = ArrayList() - backtrack(ArrayList(), nums, BooleanArray(nums.size), res) + fun permutationsI(nums: IntArray): MutableList?> { + val res = mutableListOf?>() + backtrack(mutableListOf(), nums, BooleanArray(nums.size), res) return res } ``` @@ -999,11 +999,11 @@ comments: true ) { // 当状态长度等于元素数量时,记录解 if (state.size == choices.size) { - res.add(ArrayList(state)) + res.add(state.toMutableList()) return } // 遍历所有选择 - val duplicated: MutableSet = HashSet() + val duplicated = HashSet() for (i in choices.indices) { val choice = choices[i] // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 @@ -1023,8 +1023,8 @@ comments: true /* 全排列 II */ fun permutationsII(nums: IntArray): MutableList?> { - val res: MutableList?> = ArrayList() - backtrack(ArrayList(), nums, BooleanArray(nums.size), res) + val res = mutableListOf?>() + backtrack(mutableListOf(), nums, BooleanArray(nums.size), res) return res } ``` diff --git a/docs/chapter_backtracking/subset_sum_problem.md b/docs/chapter_backtracking/subset_sum_problem.md index 749d64ad1..2cb091379 100644 --- a/docs/chapter_backtracking/subset_sum_problem.md +++ b/docs/chapter_backtracking/subset_sum_problem.md @@ -435,11 +435,11 @@ comments: true target: Int, total: Int, choices: IntArray, - res: MutableList?> + res: MutableList?> ) { // 子集和等于 target 时,记录解 if (total == target) { - res.add(ArrayList(state)) + res.add(state.toMutableList()) return } // 遍历所有选择 @@ -458,10 +458,10 @@ comments: true } /* 求解子集和 I(包含重复子集) */ - fun subsetSumINaive(nums: IntArray, target: Int): List?> { - val state: MutableList = ArrayList() // 状态(子集) + fun subsetSumINaive(nums: IntArray, target: Int): MutableList?> { + val state = mutableListOf() // 状态(子集) val total = 0 // 子集和 - val res: MutableList?> = ArrayList() // 结果列表(子集列表) + val res = mutableListOf?>() // 结果列表(子集列表) backtrack(state, target, total, nums, res) return res } @@ -973,11 +973,11 @@ comments: true target: Int, choices: IntArray, start: Int, - res: MutableList?> + res: MutableList?> ) { // 子集和等于 target 时,记录解 if (target == 0) { - res.add(ArrayList(state)) + res.add(state.toMutableList()) return } // 遍历所有选择 @@ -998,11 +998,11 @@ comments: true } /* 求解子集和 I */ - fun subsetSumI(nums: IntArray, target: Int): List?> { - val state: MutableList = ArrayList() // 状态(子集) - Arrays.sort(nums) // 对 nums 进行排序 + fun subsetSumI(nums: IntArray, target: Int): MutableList?> { + val state = mutableListOf() // 状态(子集) + nums.sort() // 对 nums 进行排序 val start = 0 // 遍历起始点 - val res: MutableList?> = ArrayList() // 结果列表(子集列表) + val res = mutableListOf?>() // 结果列表(子集列表) backtrack(state, target, nums, start, res) return res } @@ -1555,11 +1555,11 @@ comments: true target: Int, choices: IntArray, start: Int, - res: MutableList?> + res: MutableList?> ) { // 子集和等于 target 时,记录解 if (target == 0) { - res.add(ArrayList(state)) + res.add(state.toMutableList()) return } // 遍历所有选择 @@ -1585,11 +1585,11 @@ comments: true } /* 求解子集和 II */ - fun subsetSumII(nums: IntArray, target: Int): List?> { - val state: MutableList = ArrayList() // 状态(子集) - Arrays.sort(nums) // 对 nums 进行排序 + fun subsetSumII(nums: IntArray, target: Int): MutableList?> { + val state = mutableListOf() // 状态(子集) + nums.sort() // 对 nums 进行排序 val start = 0 // 遍历起始点 - val res: MutableList?> = ArrayList() // 结果列表(子集列表) + val res = mutableListOf?>() // 结果列表(子集列表) backtrack(state, target, nums, start, res) return res } diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index 6929ebf51..a85784201 100755 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -1915,9 +1915,9 @@ $$ /* 平方阶 */ fun quadratic(n: Int) { // 矩阵占用 O(n^2) 空间 - val numMatrix: Array?> = arrayOfNulls(n) + val numMatrix = arrayOfNulls?>(n) // 二维列表占用 O(n^2) 空间 - val numList: MutableList> = arrayListOf() + val numList = mutableListOf>() for (i in 0..() for (j in 0.. int[] val res = arrayOfNulls(n) for (i in 0.. 表 3-2   指数位含义

diff --git a/docs/chapter_hashing/hash_collision.md b/docs/chapter_hashing/hash_collision.md index 47bd6748e..19a3ab807 100644 --- a/docs/chapter_hashing/hash_collision.md +++ b/docs/chapter_hashing/hash_collision.md @@ -2824,6 +2824,9 @@ comments: true free(pair); } } + free(hashMap->buckets); + free(hashMap->TOMBSTONE); + free(hashMap); } /* 哈希函数 */ diff --git a/docs/chapter_stack_and_queue/deque.md b/docs/chapter_stack_and_queue/deque.md index de548afd2..5900e6fe7 100644 --- a/docs/chapter_stack_and_queue/deque.md +++ b/docs/chapter_stack_and_queue/deque.md @@ -1826,8 +1826,8 @@ comments: true if (fNext) { fNext->prev = NULL; deque->front->next = NULL; - delDoublyListNode(deque->front); } + delDoublyListNode(deque->front); deque->front = fNext; // 更新头节点 } // 队尾出队操作 @@ -1837,8 +1837,8 @@ comments: true if (rPrev) { rPrev->next = NULL; deque->rear->prev = NULL; - delDoublyListNode(deque->rear); } + delDoublyListNode(deque->rear); deque->rear = rPrev; // 更新尾节点 } deque->queSize--; // 更新队列长度 diff --git a/en/docs/chapter_array_and_linkedlist/array.md b/en/docs/chapter_array_and_linkedlist/array.md index b41ad461d..9cf56cc61 100755 --- a/en/docs/chapter_array_and_linkedlist/array.md +++ b/en/docs/chapter_array_and_linkedlist/array.md @@ -694,8 +694,8 @@ Please note that after deletion, the former last element becomes "meaningless," ### 删除索引 index 处的元素 ### def remove(nums, index) # 把索引 index 之后的所有元素向前移动一位 - for i in index...nums.length - nums[i] = nums[i + 1] || 0 + for i in index...(nums.length - 1) + nums[i] = nums[i + 1] end end ``` @@ -934,7 +934,7 @@ In most programming languages, we can traverse an array either by using indices count += nums[i] } // 直接遍历数组元素 - for (j: Int in nums) { + for (j in nums) { count += j } } @@ -1140,7 +1140,8 @@ Because arrays are linear data structures, this operation is commonly referred t /* 在数组中查找指定元素 */ fun find(nums: IntArray, target: Int): Int { for (i in nums.indices) { - if (nums[i] == target) return i + if (nums[i] == target) + return i } return -1 } diff --git a/en/docs/chapter_array_and_linkedlist/linked_list.md b/en/docs/chapter_array_and_linkedlist/linked_list.md index bae9b71a8..ff49de71d 100755 --- a/en/docs/chapter_array_and_linkedlist/linked_list.md +++ b/en/docs/chapter_array_and_linkedlist/linked_list.md @@ -544,7 +544,7 @@ By comparison, inserting an element into an array has a time complexity of $O(n) === "Kotlin" ```kotlin title="linked_list.kt" - /* 在链表的节点 n0 之后插入节点p */ + /* 在链表的节点 n0 之后插入节点 P */ fun insert(n0: ListNode?, p: ListNode?) { val n1 = n0?.next p?.next = n1 @@ -758,9 +758,11 @@ It's important to note that even though node `P` continues to point to `n1` afte ```kotlin title="linked_list.kt" /* 删除链表的节点 n0 之后的首个节点 */ fun remove(n0: ListNode?) { - val p = n0?.next + if (n0?.next == null) + return + val p = n0.next val n1 = p?.next - n0?.next = n1 + n0.next = n1 } ``` @@ -965,7 +967,9 @@ It's important to note that even though node `P` continues to point to `n1` afte fun access(head: ListNode?, index: Int): ListNode? { var h = head for (i in 0..= size) - throw IndexOutOfBoundsException() + throw IndexOutOfBoundsException("索引越界") return arr[index] } @@ -2110,7 +2110,7 @@ To enhance our understanding of how lists work, we will attempt to implement a s fun remove(index: Int): Int { if (index < 0 || index >= size) throw IndexOutOfBoundsException("索引越界") - val num: Int = arr[index] + val num = arr[index] // 将将索引 index 之后的元素都向前移动一位 for (j in index..?> = arrayOfNulls(n) + val numMatrix = arrayOfNulls?>(n) // 二维列表占用 O(n^2) 空间 - val numList: MutableList> = arrayListOf() + val numList = mutableListOf>() for (i in 0..() for (j in 0.. int[] val res = arrayOfNulls(n) for (i in 0.. Table 3-2   Meaning of exponent bits

diff --git a/en/docs/chapter_hashing/hash_collision.md b/en/docs/chapter_hashing/hash_collision.md index 0924e0023..c8bf9e90b 100644 --- a/en/docs/chapter_hashing/hash_collision.md +++ b/en/docs/chapter_hashing/hash_collision.md @@ -2824,6 +2824,9 @@ The code below implements an open addressing (linear probing) hash table with la free(pair); } } + free(hashMap->buckets); + free(hashMap->TOMBSTONE); + free(hashMap); } /* 哈希函数 */ diff --git a/en/docs/chapter_stack_and_queue/deque.md b/en/docs/chapter_stack_and_queue/deque.md index c8123ce64..1c5dc9dfa 100644 --- a/en/docs/chapter_stack_and_queue/deque.md +++ b/en/docs/chapter_stack_and_queue/deque.md @@ -1797,8 +1797,8 @@ The implementation code is as follows: if (fNext) { fNext->prev = NULL; deque->front->next = NULL; - delDoublyListNode(deque->front); } + delDoublyListNode(deque->front); deque->front = fNext; // 更新头节点 } // 队尾出队操作 @@ -1808,8 +1808,8 @@ The implementation code is as follows: if (rPrev) { rPrev->next = NULL; deque->rear->prev = NULL; - delDoublyListNode(deque->rear); } + delDoublyListNode(deque->rear); deque->rear = rPrev; // 更新尾节点 } deque->queSize--; // 更新队列长度