diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 626758c70..79f74b126 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -813,6 +813,7 @@ comments: true fun remove(n0: ListNode?) { if (n0?.next == null) return + // n0 -> P -> n1 val p = n0.next val n1 = p?.next n0.next = n1 diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index 0abf3d22d..d458f7254 100755 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -385,10 +385,10 @@ comments: true nums.Add(4); /* 在中间插入元素 */ - nums.Insert(3, 6); + nums.Insert(3, 6); // 在索引 3 处插入数字 6 /* 删除元素 */ - nums.RemoveAt(3); + nums.RemoveAt(3); // 删除索引 3 处的元素 ``` === "Go" @@ -445,10 +445,10 @@ comments: true nums.push(4); /* 在中间插入元素 */ - nums.splice(3, 0, 6); + nums.splice(3, 0, 6); // 在索引 3 处插入数字 6 /* 删除元素 */ - nums.splice(3, 1); + nums.splice(3, 1); // 删除索引 3 处的元素 ``` === "TS" @@ -465,10 +465,10 @@ comments: true nums.push(4); /* 在中间插入元素 */ - nums.splice(3, 0, 6); + nums.splice(3, 0, 6); // 在索引 3 处插入数字 6 /* 删除元素 */ - nums.splice(3, 1); + nums.splice(3, 1); // 删除索引 3 处的元素 ``` === "Dart" diff --git a/docs/chapter_computational_complexity/iteration_and_recursion.md b/docs/chapter_computational_complexity/iteration_and_recursion.md index 7430d6755..3288f4c4a 100644 --- a/docs/chapter_computational_complexity/iteration_and_recursion.md +++ b/docs/chapter_computational_complexity/iteration_and_recursion.md @@ -1992,6 +1992,7 @@ comments: true var res = 0 // 递: 递归调用 for (i in n downTo 0) { + // 通过“入栈操作”模拟“递” stack.push(i) } // 归: 返回结果 diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 53b002a28..e49182d5f 100755 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -1431,7 +1431,6 @@ $$ /* 线性阶 */ fun linear(n: Int): Int { var count = 0 - // 循环次数与数组长度成正比 for (i in 0.. nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] - nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] } + val temp = nums[j] + nums[j] = nums[j + 1] + nums[j + 1] = temp count += 3 // 元素交换包含 3 个单元操作 } } @@ -2452,8 +2453,8 @@ $$ /* 指数阶(循环实现) */ fun exponential(n: Int): Int { var count = 0 - // 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) var base = 1 + // 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) for (i in 0..(n) for (i in 0.. n { continue } + // 尝试:做出选择,更新状态 backtrack(choices: choices, state: state + choice, n: n, res: &res) + // 回退 } } @@ -1597,7 +1599,9 @@ $$ var a = 1 var b = 2 for (i in 3..n) { - b += a.also { a = b } + val temp = b + b += a + a = temp } return b } diff --git a/docs/chapter_dynamic_programming/knapsack_problem.md b/docs/chapter_dynamic_programming/knapsack_problem.md index 347b598fd..98375ccee 100644 --- a/docs/chapter_dynamic_programming/knapsack_problem.md +++ b/docs/chapter_dynamic_programming/knapsack_problem.md @@ -1036,11 +1036,7 @@ $$ ```kotlin title="knapsack.kt" /* 0-1 背包:动态规划 */ - fun knapsackDP( - wgt: IntArray, - _val: IntArray, - cap: Int - ): Int { + fun knapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int { val n = wgt.size // 初始化 dp 表 val dp = Array(n + 1) { IntArray(cap + 1) } @@ -1429,11 +1425,7 @@ $$ ```kotlin title="knapsack.kt" /* 0-1 背包:空间优化后的动态规划 */ - fun knapsackDPComp( - wgt: IntArray, - _val: IntArray, - cap: Int - ): Int { + fun knapsackDPComp(wgt: IntArray, _val: IntArray, cap: Int): Int { val n = wgt.size // 初始化 dp 表 val dp = IntArray(cap + 1) @@ -1443,8 +1435,7 @@ $$ for (c in cap downTo 1) { if (wgt[i - 1] <= c) { // 不选和选物品 i 这两种方案的较大值 - dp[c] = - max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1]) + dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1]) } } } diff --git a/docs/chapter_graph/graph_operations.md b/docs/chapter_graph/graph_operations.md index 114725456..e95b15554 100644 --- a/docs/chapter_graph/graph_operations.md +++ b/docs/chapter_graph/graph_operations.md @@ -1103,8 +1103,8 @@ comments: true if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw IndexOutOfBoundsException() // 在无向图中,邻接矩阵关于主对角线对称,即满足 (i, j) == (j, i) - adjMat[i][j] = 1; - adjMat[j][i] = 1; + adjMat[i][j] = 1 + adjMat[j][i] = 1 } /* 删除边 */ @@ -1113,15 +1113,15 @@ comments: true // 索引越界与相等处理 if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw IndexOutOfBoundsException() - adjMat[i][j] = 0; - adjMat[j][i] = 0; + adjMat[i][j] = 0 + adjMat[j][i] = 0 } /* 打印邻接矩阵 */ fun print() { print("顶点列表 = ") - println(vertices); - println("邻接矩阵 ="); + println(vertices) + println("邻接矩阵 =") printMatrix(adjMat) } } @@ -2167,9 +2167,9 @@ comments: true init { // 添加所有顶点和边 for (edge in edges) { - addVertex(edge[0]!!); - addVertex(edge[1]!!); - addEdge(edge[0]!!, edge[1]!!); + addVertex(edge[0]!!) + addVertex(edge[1]!!) + addEdge(edge[0]!!, edge[1]!!) } } @@ -2184,7 +2184,7 @@ comments: true throw IllegalArgumentException() // 添加边 vet1 - vet2 adjList[vet1]?.add(vet2) - adjList[vet2]?.add(vet1); + adjList[vet2]?.add(vet1) } /* 删除边 */ @@ -2192,8 +2192,8 @@ comments: true if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2) throw IllegalArgumentException() // 删除边 vet1 - vet2 - adjList[vet1]?.remove(vet2); - adjList[vet2]?.remove(vet1); + adjList[vet1]?.remove(vet2) + adjList[vet2]?.remove(vet1) } /* 添加顶点 */ @@ -2209,7 +2209,7 @@ comments: true if (!adjList.containsKey(vet)) throw IllegalArgumentException() // 在邻接表中删除顶点 vet 对应的链表 - adjList.remove(vet); + adjList.remove(vet) // 遍历其他顶点的链表,删除所有包含 vet 的边 for (list in adjList.values) { list.remove(vet) diff --git a/docs/chapter_hashing/hash_algorithm.md b/docs/chapter_hashing/hash_algorithm.md index 81c8654b8..5ef5103c0 100644 --- a/docs/chapter_hashing/hash_algorithm.md +++ b/docs/chapter_hashing/hash_algorithm.md @@ -560,6 +560,7 @@ index = hash(key) % capacity /* 加法哈希 */ fun addHash(key: String): Int { var hash = 0L + val MODULUS = 1000000007 for (c in key.toCharArray()) { hash = (hash + c.code) % MODULUS } @@ -569,6 +570,7 @@ index = hash(key) % capacity /* 乘法哈希 */ fun mulHash(key: String): Int { var hash = 0L + val MODULUS = 1000000007 for (c in key.toCharArray()) { hash = (31 * hash + c.code) % MODULUS } @@ -578,6 +580,7 @@ index = hash(key) % capacity /* 异或哈希 */ fun xorHash(key: String): Int { var hash = 0 + val MODULUS = 1000000007 for (c in key.toCharArray()) { hash = hash xor c.code } @@ -587,6 +590,7 @@ index = hash(key) % capacity /* 旋转哈希 */ fun rotHash(key: String): Int { var hash = 0L + val MODULUS = 1000000007 for (c in key.toCharArray()) { hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS } diff --git a/docs/chapter_hashing/hash_collision.md b/docs/chapter_hashing/hash_collision.md index 067c26d9b..a87f7d414 100644 --- a/docs/chapter_hashing/hash_collision.md +++ b/docs/chapter_hashing/hash_collision.md @@ -1312,7 +1312,7 @@ comments: true ```kotlin title="hash_map_chaining.kt" /* 链式地址哈希表 */ - class HashMapChaining() { + class HashMapChaining { var size: Int // 键值对数量 var capacity: Int // 哈希表容量 val loadThres: Double // 触发扩容的负载因子阈值 diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 7167dd21f..ceec027da 100755 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -1646,7 +1646,8 @@ index = hash(key) % capacity fun valueSet(): MutableList { val valueSet = mutableListOf() for (pair in buckets) { - pair?.let { valueSet.add(it._val) } + if (pair != null) + valueSet.add(pair._val) } return valueSet } @@ -1656,7 +1657,7 @@ index = hash(key) % capacity for (kv in pairSet()) { val key = kv.key val _val = kv._val - println("${key} -> ${_val}") + println("$key -> $_val") } } } diff --git a/docs/chapter_heap/build_heap.md b/docs/chapter_heap/build_heap.md index 93a931e68..76508f966 100644 --- a/docs/chapter_heap/build_heap.md +++ b/docs/chapter_heap/build_heap.md @@ -220,7 +220,9 @@ comments: true /* 交换元素 */ private fun swap(i: Int, j: Int) { - maxHeap[i] = maxHeap[j].also { maxHeap[j] = maxHeap[i] } + val temp = maxHeap[i] + maxHeap[i] = maxHeap[j] + maxHeap[j] = temp } /* 获取堆大小 */ diff --git a/docs/chapter_heap/heap.md b/docs/chapter_heap/heap.md index 678f242a5..cc10ba512 100644 --- a/docs/chapter_heap/heap.md +++ b/docs/chapter_heap/heap.md @@ -650,7 +650,7 @@ comments: true /* 获取父节点的索引 */ int parent(MaxHeap *maxHeap, int i) { - return (i - 1) / 2; + return (i - 1) / 2; // 向下取整 } ``` diff --git a/docs/chapter_sorting/bubble_sort.md b/docs/chapter_sorting/bubble_sort.md index 072422267..beb31f559 100755 --- a/docs/chapter_sorting/bubble_sort.md +++ b/docs/chapter_sorting/bubble_sort.md @@ -264,7 +264,9 @@ comments: true for (j in 0.. nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] - nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] } + val temp = nums[j] + nums[j] = nums[j + 1] + nums[j + 1] = temp } } } @@ -571,7 +573,9 @@ comments: true for (j in 0.. nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] - nums[j] = nums[j + 1].also { nums[j] = nums[j + 1] } + val temp = nums[j] + nums[j] = nums[j + 1] + nums[j + 1] = temp flag = true // 记录交换元素 } } diff --git a/docs/chapter_sorting/heap_sort.md b/docs/chapter_sorting/heap_sort.md index 02db02d9c..209112721 100644 --- a/docs/chapter_sorting/heap_sort.md +++ b/docs/chapter_sorting/heap_sort.md @@ -553,7 +553,9 @@ comments: true if (ma == i) break // 交换两节点 - nums[i] = nums[ma].also { nums[ma] = nums[i] } + val temp = nums[i] + nums[i] = nums[ma] + nums[ma] = temp // 循环向下堆化 i = ma } @@ -568,7 +570,9 @@ comments: true // 从堆中提取最大元素,循环 n-1 轮 for (i in nums.size - 1 downTo 1) { // 交换根节点与最右叶节点(交换首元素与尾元素) - nums[0] = nums[i].also { nums[i] = nums[0] } + val temp = nums[0] + nums[0] = nums[i] + nums[i] = temp // 以根节点为起点,从顶至底进行堆化 siftDown(nums, i, 0) } diff --git a/docs/chapter_sorting/quick_sort.md b/docs/chapter_sorting/quick_sort.md index 2bff19b7b..f3acd7b1a 100755 --- a/docs/chapter_sorting/quick_sort.md +++ b/docs/chapter_sorting/quick_sort.md @@ -328,7 +328,9 @@ comments: true ```kotlin title="quick_sort.kt" /* 元素交换 */ fun swap(nums: IntArray, i: Int, j: Int) { - nums[i] = nums[j].also { nums[j] = nums[i] } + val temp = nums[i] + nums[i] = nums[j] + nums[j] = temp } /* 哨兵划分 */ diff --git a/docs/chapter_sorting/selection_sort.md b/docs/chapter_sorting/selection_sort.md index 9b340c8ba..983e01343 100644 --- a/docs/chapter_sorting/selection_sort.md +++ b/docs/chapter_sorting/selection_sort.md @@ -296,7 +296,9 @@ comments: true k = j // 记录最小元素的索引 } // 将该最小元素与未排序区间的首个元素交换 - nums[i] = nums[k].also { nums[k] = nums[i] } + val temp = nums[i] + nums[i] = nums[k] + nums[k] = temp } } ``` diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 9d7324421..aa7c26a50 100755 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -1092,7 +1092,7 @@ comments: true fun pop(): Int? { val num = peek() stackPeek = stackPeek?.next - stkSize--; + stkSize-- return num } diff --git a/docs/chapter_tree/array_representation_of_tree.md b/docs/chapter_tree/array_representation_of_tree.md index ea64d31b9..d3bdf85a9 100644 --- a/docs/chapter_tree/array_representation_of_tree.md +++ b/docs/chapter_tree/array_representation_of_tree.md @@ -125,7 +125,7 @@ comments: true ```kotlin title="" /* 二叉树的数组表示 */ // 使用 null 来表示空位 - val tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 ) + val tree = arrayOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 ) ``` === "Ruby" @@ -1172,7 +1172,7 @@ comments: true === "Kotlin" ```kotlin title="array_binary_tree.kt" - /* 构造方法 */ + /* 数组表示下的二叉树类 */ class ArrayBinaryTree(val tree: MutableList) { /* 列表容量 */ fun size(): Int { diff --git a/docs/chapter_tree/binary_tree_traversal.md b/docs/chapter_tree/binary_tree_traversal.md index 1ef8576b5..41065ebd0 100755 --- a/docs/chapter_tree/binary_tree_traversal.md +++ b/docs/chapter_tree/binary_tree_traversal.md @@ -305,7 +305,7 @@ comments: true val list = mutableListOf() while (queue.isNotEmpty()) { val node = queue.poll() // 队列出队 - list.add(node?._val!!) // 保存节点值 + list.add(node?._val!!) // 保存节点值 if (node.left != null) queue.offer(node.left) // 左子节点入队 if (node.right != null) diff --git a/en/docs/chapter_array_and_linkedlist/linked_list.md b/en/docs/chapter_array_and_linkedlist/linked_list.md index e28168773..0da279f91 100755 --- a/en/docs/chapter_array_and_linkedlist/linked_list.md +++ b/en/docs/chapter_array_and_linkedlist/linked_list.md @@ -760,6 +760,7 @@ It's important to note that even though node `P` continues to point to `n1` afte fun remove(n0: ListNode?) { if (n0?.next == null) return + // n0 -> P -> n1 val p = n0.next val n1 = p?.next n0.next = n1 diff --git a/en/docs/chapter_computational_complexity/iteration_and_recursion.md b/en/docs/chapter_computational_complexity/iteration_and_recursion.md index 2c247982c..5f8cdc981 100644 --- a/en/docs/chapter_computational_complexity/iteration_and_recursion.md +++ b/en/docs/chapter_computational_complexity/iteration_and_recursion.md @@ -1992,6 +1992,7 @@ Therefore, **we can use an explicit stack to simulate the behavior of the call s var res = 0 // 递: 递归调用 for (i in n downTo 0) { + // 通过“入栈操作”模拟“递” stack.push(i) } // 归: 返回结果 diff --git a/en/docs/chapter_computational_complexity/time_complexity.md b/en/docs/chapter_computational_complexity/time_complexity.md index 2db99ceba..a70cf440a 100644 --- a/en/docs/chapter_computational_complexity/time_complexity.md +++ b/en/docs/chapter_computational_complexity/time_complexity.md @@ -1319,7 +1319,6 @@ Linear order indicates the number of operations grows linearly with the input da /* 线性阶 */ fun linear(n: Int): Int { var count = 0 - // 循环次数与数组长度成正比 for (i in 0.. nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] - nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] } + val temp = nums[j] + nums[j] = nums[j + 1] + nums[j + 1] = temp count += 3 // 元素交换包含 3 个单元操作 } } @@ -2340,8 +2341,8 @@ The following image and code simulate the cell division process, with a time com /* 指数阶(循环实现) */ fun exponential(n: Int): Int { var count = 0 - // 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) var base = 1 + // 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) for (i in 0..(n) for (i in 0..= size() || j >= size() || i == j) throw IndexOutOfBoundsException() // 在无向图中,邻接矩阵关于主对角线对称,即满足 (i, j) == (j, i) - adjMat[i][j] = 1; - adjMat[j][i] = 1; + adjMat[i][j] = 1 + adjMat[j][i] = 1 } /* 删除边 */ @@ -1113,15 +1113,15 @@ Below is the implementation code for graphs represented using an adjacency matri // 索引越界与相等处理 if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw IndexOutOfBoundsException() - adjMat[i][j] = 0; - adjMat[j][i] = 0; + adjMat[i][j] = 0 + adjMat[j][i] = 0 } /* 打印邻接矩阵 */ fun print() { print("顶点列表 = ") - println(vertices); - println("邻接矩阵 ="); + println(vertices) + println("邻接矩阵 =") printMatrix(adjMat) } } @@ -2167,9 +2167,9 @@ Additionally, we use the `Vertex` class to represent vertices in the adjacency l init { // 添加所有顶点和边 for (edge in edges) { - addVertex(edge[0]!!); - addVertex(edge[1]!!); - addEdge(edge[0]!!, edge[1]!!); + addVertex(edge[0]!!) + addVertex(edge[1]!!) + addEdge(edge[0]!!, edge[1]!!) } } @@ -2184,7 +2184,7 @@ Additionally, we use the `Vertex` class to represent vertices in the adjacency l throw IllegalArgumentException() // 添加边 vet1 - vet2 adjList[vet1]?.add(vet2) - adjList[vet2]?.add(vet1); + adjList[vet2]?.add(vet1) } /* 删除边 */ @@ -2192,8 +2192,8 @@ Additionally, we use the `Vertex` class to represent vertices in the adjacency l if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2) throw IllegalArgumentException() // 删除边 vet1 - vet2 - adjList[vet1]?.remove(vet2); - adjList[vet2]?.remove(vet1); + adjList[vet1]?.remove(vet2) + adjList[vet2]?.remove(vet1) } /* 添加顶点 */ @@ -2209,7 +2209,7 @@ Additionally, we use the `Vertex` class to represent vertices in the adjacency l if (!adjList.containsKey(vet)) throw IllegalArgumentException() // 在邻接表中删除顶点 vet 对应的链表 - adjList.remove(vet); + adjList.remove(vet) // 遍历其他顶点的链表,删除所有包含 vet 的边 for (list in adjList.values) { list.remove(vet) diff --git a/en/docs/chapter_hashing/hash_algorithm.md b/en/docs/chapter_hashing/hash_algorithm.md index ff685a9d1..bb0f91df2 100644 --- a/en/docs/chapter_hashing/hash_algorithm.md +++ b/en/docs/chapter_hashing/hash_algorithm.md @@ -560,6 +560,7 @@ The design of hash algorithms is a complex issue that requires consideration of /* 加法哈希 */ fun addHash(key: String): Int { var hash = 0L + val MODULUS = 1000000007 for (c in key.toCharArray()) { hash = (hash + c.code) % MODULUS } @@ -569,6 +570,7 @@ The design of hash algorithms is a complex issue that requires consideration of /* 乘法哈希 */ fun mulHash(key: String): Int { var hash = 0L + val MODULUS = 1000000007 for (c in key.toCharArray()) { hash = (31 * hash + c.code) % MODULUS } @@ -578,6 +580,7 @@ The design of hash algorithms is a complex issue that requires consideration of /* 异或哈希 */ fun xorHash(key: String): Int { var hash = 0 + val MODULUS = 1000000007 for (c in key.toCharArray()) { hash = hash xor c.code } @@ -587,6 +590,7 @@ The design of hash algorithms is a complex issue that requires consideration of /* 旋转哈希 */ fun rotHash(key: String): Int { var hash = 0L + val MODULUS = 1000000007 for (c in key.toCharArray()) { hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS } diff --git a/en/docs/chapter_hashing/hash_collision.md b/en/docs/chapter_hashing/hash_collision.md index 469044bf0..49df160dc 100644 --- a/en/docs/chapter_hashing/hash_collision.md +++ b/en/docs/chapter_hashing/hash_collision.md @@ -1312,7 +1312,7 @@ The code below provides a simple implementation of a separate chaining hash tabl ```kotlin title="hash_map_chaining.kt" /* 链式地址哈希表 */ - class HashMapChaining() { + class HashMapChaining { var size: Int // 键值对数量 var capacity: Int // 哈希表容量 val loadThres: Double // 触发扩容的负载因子阈值 diff --git a/en/docs/chapter_hashing/hash_map.md b/en/docs/chapter_hashing/hash_map.md index 0937369d7..f48c50fc9 100755 --- a/en/docs/chapter_hashing/hash_map.md +++ b/en/docs/chapter_hashing/hash_map.md @@ -1605,7 +1605,8 @@ The following code implements a simple hash table. Here, we encapsulate `key` an fun valueSet(): MutableList { val valueSet = mutableListOf() for (pair in buckets) { - pair?.let { valueSet.add(it._val) } + if (pair != null) + valueSet.add(pair._val) } return valueSet } @@ -1615,7 +1616,7 @@ The following code implements a simple hash table. Here, we encapsulate `key` an for (kv in pairSet()) { val key = kv.key val _val = kv._val - println("${key} -> ${_val}") + println("$key -> $_val") } } } diff --git a/en/docs/chapter_heap/build_heap.md b/en/docs/chapter_heap/build_heap.md index 83be45162..83317ac62 100644 --- a/en/docs/chapter_heap/build_heap.md +++ b/en/docs/chapter_heap/build_heap.md @@ -220,7 +220,9 @@ It's worth mentioning that **since leaf nodes have no children, they naturally f /* 交换元素 */ private fun swap(i: Int, j: Int) { - maxHeap[i] = maxHeap[j].also { maxHeap[j] = maxHeap[i] } + val temp = maxHeap[i] + maxHeap[i] = maxHeap[j] + maxHeap[j] = temp } /* 获取堆大小 */ diff --git a/en/docs/chapter_heap/heap.md b/en/docs/chapter_heap/heap.md index 27f2c0f79..03b3ef649 100644 --- a/en/docs/chapter_heap/heap.md +++ b/en/docs/chapter_heap/heap.md @@ -649,7 +649,7 @@ We can encapsulate the index mapping formula into functions for convenient later /* 获取父节点的索引 */ int parent(MaxHeap *maxHeap, int i) { - return (i - 1) / 2; + return (i - 1) / 2; // 向下取整 } ``` diff --git a/en/docs/chapter_stack_and_queue/stack.md b/en/docs/chapter_stack_and_queue/stack.md index 06555e560..22ebe4a13 100755 --- a/en/docs/chapter_stack_and_queue/stack.md +++ b/en/docs/chapter_stack_and_queue/stack.md @@ -1045,7 +1045,7 @@ Below is an example code for implementing a stack based on a linked list: fun pop(): Int? { val num = peek() stackPeek = stackPeek?.next - stkSize--; + stkSize-- return num } diff --git a/en/docs/chapter_tree/array_representation_of_tree.md b/en/docs/chapter_tree/array_representation_of_tree.md index e2e6d59c7..90fd62156 100644 --- a/en/docs/chapter_tree/array_representation_of_tree.md +++ b/en/docs/chapter_tree/array_representation_of_tree.md @@ -1172,7 +1172,7 @@ The following code implements a binary tree based on array representation, inclu === "Kotlin" ```kotlin title="array_binary_tree.kt" - /* 构造方法 */ + /* 数组表示下的二叉树类 */ class ArrayBinaryTree(val tree: MutableList) { /* 列表容量 */ fun size(): Int { diff --git a/en/docs/chapter_tree/binary_tree_traversal.md b/en/docs/chapter_tree/binary_tree_traversal.md index adf6ce22f..f1ce2a49d 100755 --- a/en/docs/chapter_tree/binary_tree_traversal.md +++ b/en/docs/chapter_tree/binary_tree_traversal.md @@ -305,7 +305,7 @@ Breadth-first traversal is usually implemented with the help of a "queue". The q val list = mutableListOf() while (queue.isNotEmpty()) { val node = queue.poll() // 队列出队 - list.add(node?._val!!) // 保存节点值 + list.add(node?._val!!) // 保存节点值 if (node.left != null) queue.offer(node.left) // 左子节点入队 if (node.right != null) diff --git a/zh-Hant/docs/chapter_array_and_linkedlist/linked_list.md b/zh-Hant/docs/chapter_array_and_linkedlist/linked_list.md index cd08c4005..93b14daa1 100755 --- a/zh-Hant/docs/chapter_array_and_linkedlist/linked_list.md +++ b/zh-Hant/docs/chapter_array_and_linkedlist/linked_list.md @@ -813,6 +813,7 @@ comments: true fun remove(n0: ListNode?) { if (n0?.next == null) return + // n0 -> P -> n1 val p = n0.next val n1 = p?.next n0.next = n1 diff --git a/zh-Hant/docs/chapter_array_and_linkedlist/list.md b/zh-Hant/docs/chapter_array_and_linkedlist/list.md index 44d5dfdcd..9884465f5 100755 --- a/zh-Hant/docs/chapter_array_and_linkedlist/list.md +++ b/zh-Hant/docs/chapter_array_and_linkedlist/list.md @@ -385,10 +385,10 @@ comments: true nums.Add(4); /* 在中間插入元素 */ - nums.Insert(3, 6); + nums.Insert(3, 6); // 在索引 3 處插入數字 6 /* 刪除元素 */ - nums.RemoveAt(3); + nums.RemoveAt(3); // 刪除索引 3 處的元素 ``` === "Go" @@ -445,10 +445,10 @@ comments: true nums.push(4); /* 在中間插入元素 */ - nums.splice(3, 0, 6); + nums.splice(3, 0, 6); // 在索引 3 處插入數字 6 /* 刪除元素 */ - nums.splice(3, 1); + nums.splice(3, 1); // 刪除索引 3 處的元素 ``` === "TS" @@ -465,10 +465,10 @@ comments: true nums.push(4); /* 在中間插入元素 */ - nums.splice(3, 0, 6); + nums.splice(3, 0, 6); // 在索引 3 處插入數字 6 /* 刪除元素 */ - nums.splice(3, 1); + nums.splice(3, 1); // 刪除索引 3 處的元素 ``` === "Dart" diff --git a/zh-Hant/docs/chapter_computational_complexity/iteration_and_recursion.md b/zh-Hant/docs/chapter_computational_complexity/iteration_and_recursion.md index 709db7c6c..72edd7d89 100644 --- a/zh-Hant/docs/chapter_computational_complexity/iteration_and_recursion.md +++ b/zh-Hant/docs/chapter_computational_complexity/iteration_and_recursion.md @@ -1992,6 +1992,7 @@ comments: true var res = 0 // 遞: 遞迴呼叫 for (i in n downTo 0) { + // 透過“入堆疊操作”模擬“遞” stack.push(i) } // 迴: 返回結果 diff --git a/zh-Hant/docs/chapter_computational_complexity/time_complexity.md b/zh-Hant/docs/chapter_computational_complexity/time_complexity.md index c749fe5f3..7ded9eca6 100755 --- a/zh-Hant/docs/chapter_computational_complexity/time_complexity.md +++ b/zh-Hant/docs/chapter_computational_complexity/time_complexity.md @@ -1431,7 +1431,6 @@ $$ /* 線性階 */ fun linear(n: Int): Int { var count = 0 - // 迴圈次數與陣列長度成正比 for (i in 0.. nums[j + 1]) { // 交換 nums[j] 與 nums[j + 1] - nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] } + val temp = nums[j] + nums[j] = nums[j + 1] + nums[j + 1] = temp count += 3 // 元素交換包含 3 個單元操作 } } @@ -2452,8 +2453,8 @@ $$ /* 指數階(迴圈實現) */ fun exponential(n: Int): Int { var count = 0 - // 細胞每輪一分為二,形成數列 1, 2, 4, 8, ..., 2^(n-1) var base = 1 + // 細胞每輪一分為二,形成數列 1, 2, 4, 8, ..., 2^(n-1) for (i in 0..(n) for (i in 0.. n { continue } + // 嘗試:做出選擇,更新狀態 backtrack(choices: choices, state: state + choice, n: n, res: &res) + // 回退 } } @@ -1597,7 +1599,9 @@ $$ var a = 1 var b = 2 for (i in 3..n) { - b += a.also { a = b } + val temp = b + b += a + a = temp } return b } diff --git a/zh-Hant/docs/chapter_dynamic_programming/knapsack_problem.md b/zh-Hant/docs/chapter_dynamic_programming/knapsack_problem.md index faf4e4626..f19287422 100644 --- a/zh-Hant/docs/chapter_dynamic_programming/knapsack_problem.md +++ b/zh-Hant/docs/chapter_dynamic_programming/knapsack_problem.md @@ -1036,11 +1036,7 @@ $$ ```kotlin title="knapsack.kt" /* 0-1 背包:動態規劃 */ - fun knapsackDP( - wgt: IntArray, - _val: IntArray, - cap: Int - ): Int { + fun knapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int { val n = wgt.size // 初始化 dp 表 val dp = Array(n + 1) { IntArray(cap + 1) } @@ -1429,11 +1425,7 @@ $$ ```kotlin title="knapsack.kt" /* 0-1 背包:空間最佳化後的動態規劃 */ - fun knapsackDPComp( - wgt: IntArray, - _val: IntArray, - cap: Int - ): Int { + fun knapsackDPComp(wgt: IntArray, _val: IntArray, cap: Int): Int { val n = wgt.size // 初始化 dp 表 val dp = IntArray(cap + 1) @@ -1443,8 +1435,7 @@ $$ for (c in cap downTo 1) { if (wgt[i - 1] <= c) { // 不選和選物品 i 這兩種方案的較大值 - dp[c] = - max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1]) + dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1]) } } } diff --git a/zh-Hant/docs/chapter_graph/graph_operations.md b/zh-Hant/docs/chapter_graph/graph_operations.md index 7a1de2233..b19a151aa 100644 --- a/zh-Hant/docs/chapter_graph/graph_operations.md +++ b/zh-Hant/docs/chapter_graph/graph_operations.md @@ -1103,8 +1103,8 @@ comments: true if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw IndexOutOfBoundsException() // 在無向圖中,鄰接矩陣關於主對角線對稱,即滿足 (i, j) == (j, i) - adjMat[i][j] = 1; - adjMat[j][i] = 1; + adjMat[i][j] = 1 + adjMat[j][i] = 1 } /* 刪除邊 */ @@ -1113,15 +1113,15 @@ comments: true // 索引越界與相等處理 if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw IndexOutOfBoundsException() - adjMat[i][j] = 0; - adjMat[j][i] = 0; + adjMat[i][j] = 0 + adjMat[j][i] = 0 } /* 列印鄰接矩陣 */ fun print() { print("頂點串列 = ") - println(vertices); - println("鄰接矩陣 ="); + println(vertices) + println("鄰接矩陣 =") printMatrix(adjMat) } } @@ -2167,9 +2167,9 @@ comments: true init { // 新增所有頂點和邊 for (edge in edges) { - addVertex(edge[0]!!); - addVertex(edge[1]!!); - addEdge(edge[0]!!, edge[1]!!); + addVertex(edge[0]!!) + addVertex(edge[1]!!) + addEdge(edge[0]!!, edge[1]!!) } } @@ -2184,7 +2184,7 @@ comments: true throw IllegalArgumentException() // 新增邊 vet1 - vet2 adjList[vet1]?.add(vet2) - adjList[vet2]?.add(vet1); + adjList[vet2]?.add(vet1) } /* 刪除邊 */ @@ -2192,8 +2192,8 @@ comments: true if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2) throw IllegalArgumentException() // 刪除邊 vet1 - vet2 - adjList[vet1]?.remove(vet2); - adjList[vet2]?.remove(vet1); + adjList[vet1]?.remove(vet2) + adjList[vet2]?.remove(vet1) } /* 新增頂點 */ @@ -2209,7 +2209,7 @@ comments: true if (!adjList.containsKey(vet)) throw IllegalArgumentException() // 在鄰接表中刪除頂點 vet 對應的鏈結串列 - adjList.remove(vet); + adjList.remove(vet) // 走訪其他頂點的鏈結串列,刪除所有包含 vet 的邊 for (list in adjList.values) { list.remove(vet) diff --git a/zh-Hant/docs/chapter_hashing/hash_algorithm.md b/zh-Hant/docs/chapter_hashing/hash_algorithm.md index 8286a9791..5b24c8d9b 100644 --- a/zh-Hant/docs/chapter_hashing/hash_algorithm.md +++ b/zh-Hant/docs/chapter_hashing/hash_algorithm.md @@ -560,6 +560,7 @@ index = hash(key) % capacity /* 加法雜湊 */ fun addHash(key: String): Int { var hash = 0L + val MODULUS = 1000000007 for (c in key.toCharArray()) { hash = (hash + c.code) % MODULUS } @@ -569,6 +570,7 @@ index = hash(key) % capacity /* 乘法雜湊 */ fun mulHash(key: String): Int { var hash = 0L + val MODULUS = 1000000007 for (c in key.toCharArray()) { hash = (31 * hash + c.code) % MODULUS } @@ -578,6 +580,7 @@ index = hash(key) % capacity /* 互斥或雜湊 */ fun xorHash(key: String): Int { var hash = 0 + val MODULUS = 1000000007 for (c in key.toCharArray()) { hash = hash xor c.code } @@ -587,6 +590,7 @@ index = hash(key) % capacity /* 旋轉雜湊 */ fun rotHash(key: String): Int { var hash = 0L + val MODULUS = 1000000007 for (c in key.toCharArray()) { hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS } diff --git a/zh-Hant/docs/chapter_hashing/hash_collision.md b/zh-Hant/docs/chapter_hashing/hash_collision.md index 1a05aa856..2d2caf172 100644 --- a/zh-Hant/docs/chapter_hashing/hash_collision.md +++ b/zh-Hant/docs/chapter_hashing/hash_collision.md @@ -1312,7 +1312,7 @@ comments: true ```kotlin title="hash_map_chaining.kt" /* 鏈式位址雜湊表 */ - class HashMapChaining() { + class HashMapChaining { var size: Int // 鍵值對數量 var capacity: Int // 雜湊表容量 val loadThres: Double // 觸發擴容的負載因子閾值 diff --git a/zh-Hant/docs/chapter_hashing/hash_map.md b/zh-Hant/docs/chapter_hashing/hash_map.md index 9c7aea2d1..b1f4a1b18 100755 --- a/zh-Hant/docs/chapter_hashing/hash_map.md +++ b/zh-Hant/docs/chapter_hashing/hash_map.md @@ -1464,7 +1464,7 @@ index = hash(key) % capacity /* 建構子 */ ArrayHashMap *newArrayHashMap() { ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap)); - for (int i = 0; i < MAX_SIZE; i++) { + for (int i=0; i < MAX_SIZE; i++) { hmap->buckets[i] = NULL; } return hmap; @@ -1646,7 +1646,8 @@ index = hash(key) % capacity fun valueSet(): MutableList { val valueSet = mutableListOf() for (pair in buckets) { - pair?.let { valueSet.add(it._val) } + if (pair != null) + valueSet.add(pair._val) } return valueSet } @@ -1656,7 +1657,7 @@ index = hash(key) % capacity for (kv in pairSet()) { val key = kv.key val _val = kv._val - println("${key} -> ${_val}") + println("$key -> $_val") } } } diff --git a/zh-Hant/docs/chapter_heap/build_heap.md b/zh-Hant/docs/chapter_heap/build_heap.md index 42c765b5a..aeb6239a0 100644 --- a/zh-Hant/docs/chapter_heap/build_heap.md +++ b/zh-Hant/docs/chapter_heap/build_heap.md @@ -220,7 +220,9 @@ comments: true /* 交換元素 */ private fun swap(i: Int, j: Int) { - maxHeap[i] = maxHeap[j].also { maxHeap[j] = maxHeap[i] } + val temp = maxHeap[i] + maxHeap[i] = maxHeap[j] + maxHeap[j] = temp } /* 獲取堆積大小 */ diff --git a/zh-Hant/docs/chapter_heap/heap.md b/zh-Hant/docs/chapter_heap/heap.md index 037509ce2..e932e2388 100644 --- a/zh-Hant/docs/chapter_heap/heap.md +++ b/zh-Hant/docs/chapter_heap/heap.md @@ -650,7 +650,7 @@ comments: true /* 獲取父節點的索引 */ int parent(MaxHeap *maxHeap, int i) { - return (i - 1) / 2; + return (i - 1) / 2; // 向下取整 } ``` diff --git a/zh-Hant/docs/chapter_searching/binary_search.md b/zh-Hant/docs/chapter_searching/binary_search.md index 9657dcfe7..fd20540ea 100755 --- a/zh-Hant/docs/chapter_searching/binary_search.md +++ b/zh-Hant/docs/chapter_searching/binary_search.md @@ -339,7 +339,27 @@ comments: true === "Ruby" ```ruby title="binary_search.rb" - [class]{}-[func]{binary_search} + ### 二分搜尋(雙閉區間) ### + def binary_search(nums, target) + # 初始化雙閉區間 [0, n-1] ,即 i, j 分別指向陣列首元素、尾元素 + i, j = 0, nums.length - 1 + + # 迴圈,當搜尋區間為空時跳出(當 i > j 時為空) + while i <= j + # 理論上 Ruby 的數字可以無限大(取決於記憶體大小),無須考慮大數越界問題 + m = (i + j) / 2 # 計算中點索引 m + + if nums[m] < target + i = m + 1 # 此情況說明 target 在區間 [m+1, j] 中 + elsif nums[m] > target + j = m - 1 # 此情況說明 target 在區間 [i, m-1] 中 + else + return m # 找到目標元素,返回其索引 + end + end + + -1 # 未找到目標元素,返回 -1 + end ``` === "Zig" @@ -667,7 +687,27 @@ comments: true === "Ruby" ```ruby title="binary_search.rb" - [class]{}-[func]{binary_search_lcro} + ### 二分搜尋(左閉右開區間) ### + def binary_search_lcro(nums, target) + # 初始化左閉右開區間 [0, n) ,即 i, j 分別指向陣列首元素、尾元素+1 + i, j = 0, nums.length + + # 迴圈,當搜尋區間為空時跳出(當 i = j 時為空) + while i < j + # 計算中點索引 m + m = (i + j) / 2 + + if nums[m] < target + i = m + 1 # 此情況說明 target 在區間 [m+1, j) 中 + elsif nums[m] > target + j = m - 1 # 此情況說明 target 在區間 [i, m) 中 + else + return m # 找到目標元素,返回其索引 + end + end + + -1 # 未找到目標元素,返回 -1 + end ``` === "Zig" diff --git a/zh-Hant/docs/chapter_searching/binary_search_edge.md b/zh-Hant/docs/chapter_searching/binary_search_edge.md index e8bf1747f..f29a88954 100644 --- a/zh-Hant/docs/chapter_searching/binary_search_edge.md +++ b/zh-Hant/docs/chapter_searching/binary_search_edge.md @@ -212,7 +212,16 @@ comments: true === "Ruby" ```ruby title="binary_search_edge.rb" - [class]{}-[func]{binary_search_left_edge} + ### 二分搜尋最左一個 target ### + def binary_search_left_edge(nums, target) + # 等價於查詢 target 的插入點 + i = binary_search_insertion(nums, target) + + # 未找到 target ,返回 -1 + return -1 if i == nums.length || nums[i] != target + + i # 找到 target ,返回索引 i + end ``` === "Zig" @@ -461,7 +470,19 @@ comments: true === "Ruby" ```ruby title="binary_search_edge.rb" - [class]{}-[func]{binary_search_right_edge} + ### 二分搜尋最右一個 target ### + def binary_search_right_edge(nums, target) + # 轉化為查詢最左一個 target + 1 + i = binary_search_insertion(nums, target + 1) + + # j 指向最右一個 target ,i 指向首個大於 target 的元素 + j = i - 1 + + # 未找到 target ,返回 -1 + return -1 if j == -1 || nums[j] != target + + j # 找到 target ,返回索引 j + end ``` === "Zig" diff --git a/zh-Hant/docs/chapter_searching/binary_search_insertion.md b/zh-Hant/docs/chapter_searching/binary_search_insertion.md index 29a84ed34..9d2f4699f 100644 --- a/zh-Hant/docs/chapter_searching/binary_search_insertion.md +++ b/zh-Hant/docs/chapter_searching/binary_search_insertion.md @@ -293,7 +293,26 @@ comments: true === "Ruby" ```ruby title="binary_search_insertion.rb" - [class]{}-[func]{binary_search_insertion_simple} + ### 二分搜尋插入點(無重複元素) ### + def binary_search_insertion_simple(nums, target) + # 初始化雙閉區間 [0, n-1] + i, j = 0, nums.length - 1 + + while i <= j + # 計算中點索引 m + m = (i + j) / 2 + + if nums[m] < target + i = m + 1 # target 在區間 [m+1, j] 中 + elsif nums[m] > target + j = m - 1 # target 在區間 [i, m-1] 中 + else + return m # 找到 target ,返回插入點 m + end + end + + i # 未找到 target ,返回插入點 i + end ``` === "Zig" @@ -625,7 +644,26 @@ comments: true === "Ruby" ```ruby title="binary_search_insertion.rb" - [class]{}-[func]{binary_search_insertion} + ### 二分搜尋插入點(存在重複元素) ### + def binary_search_insertion(nums, target) + # 初始化雙閉區間 [0, n-1] + i, j = 0, nums.length - 1 + + while i <= j + # 計算中點索引 m + m = (i + j) / 2 + + if nums[m] < target + i = m + 1 # target 在區間 [m+1, j] 中 + elsif nums[m] > target + j = m - 1 # target 在區間 [i, m-1] 中 + else + j = m - 1 # 首個小於 target 的元素在區間 [i, m-1] 中 + end + end + + i # 返回插入點 i + end ``` === "Zig" diff --git a/zh-Hant/docs/chapter_searching/replace_linear_by_hashing.md b/zh-Hant/docs/chapter_searching/replace_linear_by_hashing.md index 02725ddbf..051e3e4b7 100755 --- a/zh-Hant/docs/chapter_searching/replace_linear_by_hashing.md +++ b/zh-Hant/docs/chapter_searching/replace_linear_by_hashing.md @@ -228,7 +228,17 @@ comments: true === "Ruby" ```ruby title="two_sum.rb" - [class]{}-[func]{two_sum_brute_force} + ### 方法一:暴力列舉 ### + def two_sum_brute_force(nums, target) + # 兩層迴圈,時間複雜度為 O(n^2) + for i in 0...(nums.length - 1) + for j in (i + 1)...nums.length + return [i, j] if nums[i] + nums[j] == target + end + end + + [] + end ``` === "Zig" @@ -531,7 +541,19 @@ comments: true === "Ruby" ```ruby title="two_sum.rb" - [class]{}-[func]{two_sum_hash_table} + ### 方法二:輔助雜湊表 ### + def two_sum_hash_table(nums, target) + # 輔助雜湊表,空間複雜度為 O(n) + dic = {} + # 單層迴圈,時間複雜度為 O(n) + for i in 0...nums.length + return [dic[target - nums[i]], i] if dic.has_key?(target - nums[i]) + + dic[nums[i]] = i + end + + [] + end ``` === "Zig" diff --git a/zh-Hant/docs/chapter_sorting/bubble_sort.md b/zh-Hant/docs/chapter_sorting/bubble_sort.md index bda406f4e..64f9ff562 100755 --- a/zh-Hant/docs/chapter_sorting/bubble_sort.md +++ b/zh-Hant/docs/chapter_sorting/bubble_sort.md @@ -264,7 +264,9 @@ comments: true for (j in 0.. nums[j + 1]) { // 交換 nums[j] 與 nums[j + 1] - nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] } + val temp = nums[j] + nums[j] = nums[j + 1] + nums[j + 1] = temp } } } @@ -571,7 +573,9 @@ comments: true for (j in 0.. nums[j + 1]) { // 交換 nums[j] 與 nums[j + 1] - nums[j] = nums[j + 1].also { nums[j] = nums[j + 1] } + val temp = nums[j] + nums[j] = nums[j + 1] + nums[j + 1] = temp flag = true // 記錄交換元素 } } diff --git a/zh-Hant/docs/chapter_sorting/heap_sort.md b/zh-Hant/docs/chapter_sorting/heap_sort.md index 5e98f1688..fb1e953ab 100644 --- a/zh-Hant/docs/chapter_sorting/heap_sort.md +++ b/zh-Hant/docs/chapter_sorting/heap_sort.md @@ -553,7 +553,9 @@ comments: true if (ma == i) break // 交換兩節點 - nums[i] = nums[ma].also { nums[ma] = nums[i] } + val temp = nums[i] + nums[i] = nums[ma] + nums[ma] = temp // 迴圈向下堆積化 i = ma } @@ -568,7 +570,9 @@ comments: true // 從堆積中提取最大元素,迴圈 n-1 輪 for (i in nums.size - 1 downTo 1) { // 交換根節點與最右葉節點(交換首元素與尾元素) - nums[0] = nums[i].also { nums[i] = nums[0] } + val temp = nums[0] + nums[0] = nums[i] + nums[i] = temp // 以根節點為起點,從頂至底進行堆積化 siftDown(nums, i, 0) } diff --git a/zh-Hant/docs/chapter_sorting/insertion_sort.md b/zh-Hant/docs/chapter_sorting/insertion_sort.md index 36495f0da..c6873e386 100755 --- a/zh-Hant/docs/chapter_sorting/insertion_sort.md +++ b/zh-Hant/docs/chapter_sorting/insertion_sort.md @@ -253,7 +253,21 @@ comments: true === "Ruby" ```ruby title="insertion_sort.rb" - [class]{}-[func]{insertion_sort} + ### 插入排序 ### + def insertion_sort(nums) + n = nums.length + # 外迴圈:已排序區間為 [0, i-1] + for i in 1...n + base = nums[i] + j = i - 1 + # 內迴圈:將 base 插入到已排序區間 [0, i-1] 中的正確位置 + while j >= 0 && nums[j] > base + nums[j + 1] = nums[j] # 將 nums[j] 向右移動一位 + j -= 1 + end + nums[j + 1] = base # 將 base 賦值到正確位置 + end + end ``` === "Zig" diff --git a/zh-Hant/docs/chapter_sorting/quick_sort.md b/zh-Hant/docs/chapter_sorting/quick_sort.md index 722f002f9..212ba8e7a 100755 --- a/zh-Hant/docs/chapter_sorting/quick_sort.md +++ b/zh-Hant/docs/chapter_sorting/quick_sort.md @@ -302,19 +302,16 @@ comments: true nums[j] = tmp; } - /* 快速排序類別 */ - // 快速排序類別-哨兵劃分 + /* 哨兵劃分 */ int partition(int nums[], int left, int right) { // 以 nums[left] 為基準數 int i = left, j = right; while (i < j) { while (i < j && nums[j] >= nums[left]) { - // 從右向左找首個小於基準數的元素 - j--; + j--; // 從右向左找首個小於基準數的元素 } while (i < j && nums[i] <= nums[left]) { - // 從左向右找首個大於基準數的元素 - i++; + i++; // 從左向右找首個大於基準數的元素 } // 交換這兩個元素 swap(nums, i, j); @@ -331,7 +328,9 @@ comments: true ```kotlin title="quick_sort.kt" /* 元素交換 */ fun swap(nums: IntArray, i: Int, j: Int) { - nums[i] = nums[j].also { nums[j] = nums[i] } + val temp = nums[i] + nums[i] = nums[j] + nums[j] = temp } /* 哨兵劃分 */ @@ -563,30 +562,7 @@ comments: true === "C" ```c title="quick_sort.c" - /* 快速排序類別 */ - // 快速排序類別-哨兵劃分 - int partition(int nums[], int left, int right) { - // 以 nums[left] 為基準數 - int i = left, j = right; - while (i < j) { - while (i < j && nums[j] >= nums[left]) { - // 從右向左找首個小於基準數的元素 - j--; - } - while (i < j && nums[i] <= nums[left]) { - // 從左向右找首個大於基準數的元素 - i++; - } - // 交換這兩個元素 - swap(nums, i, j); - } - // 將基準數交換至兩子陣列的分界線 - swap(nums, i, left); - // 返回基準數的索引 - return i; - } - - // 快速排序類別-快速排序 + /* 快速排序 */ void quickSort(int nums[], int left, int right) { // 子陣列長度為 1 時終止遞迴 if (left >= right) { @@ -1022,8 +998,7 @@ comments: true === "C" ```c title="quick_sort.c" - /* 快速排序類別(中位基準數最佳化) */ - // 選取三個候選元素的中位數 + /* 選取三個候選元素的中位數 */ int medianThree(int nums[], int left, int mid, int right) { int l = nums[left], m = nums[mid], r = nums[right]; if ((l <= m && m <= r) || (r <= m && m <= l)) @@ -1033,7 +1008,7 @@ comments: true return right; } - /* 哨兵劃分(三數取中值) */ + /* 哨兵劃分(三數取中值) */ int partitionMedian(int nums[], int left, int right) { // 選取三個候選元素的中位數 int med = medianThree(nums, left, (left + right) / 2, right); @@ -1354,8 +1329,7 @@ comments: true === "C" ```c title="quick_sort.c" - /* 快速排序類別(尾遞迴最佳化) */ - // 快速排序(尾遞迴最佳化) + /* 快速排序(尾遞迴最佳化) */ void quickSortTailCall(int nums[], int left, int right) { // 子陣列長度為 1 時終止 while (left < right) { @@ -1363,11 +1337,15 @@ comments: true int pivot = partition(nums, left, right); // 對兩個子陣列中較短的那個執行快速排序 if (pivot - left < right - pivot) { - quickSortTailCall(nums, left, pivot - 1); // 遞迴排序左子陣列 - left = pivot + 1; // 剩餘未排序區間為 [pivot + 1, right] + // 遞迴排序左子陣列 + quickSortTailCall(nums, left, pivot - 1); + // 剩餘未排序區間為 [pivot + 1, right] + left = pivot + 1; } else { - quickSortTailCall(nums, pivot + 1, right); // 遞迴排序右子陣列 - right = pivot - 1; // 剩餘未排序區間為 [left, pivot - 1] + // 遞迴排序右子陣列 + quickSortTailCall(nums, pivot + 1, right); + // 剩餘未排序區間為 [left, pivot - 1] + right = pivot - 1; } } } diff --git a/zh-Hant/docs/chapter_sorting/selection_sort.md b/zh-Hant/docs/chapter_sorting/selection_sort.md index 89c465eec..f614cfcc8 100644 --- a/zh-Hant/docs/chapter_sorting/selection_sort.md +++ b/zh-Hant/docs/chapter_sorting/selection_sort.md @@ -296,7 +296,9 @@ comments: true k = j // 記錄最小元素的索引 } // 將該最小元素與未排序區間的首個元素交換 - nums[i] = nums[k].also { nums[k] = nums[i] } + val temp = nums[i] + nums[i] = nums[k] + nums[k] = temp } } ``` diff --git a/zh-Hant/docs/chapter_stack_and_queue/stack.md b/zh-Hant/docs/chapter_stack_and_queue/stack.md index 87f2c58d4..475e05082 100755 --- a/zh-Hant/docs/chapter_stack_and_queue/stack.md +++ b/zh-Hant/docs/chapter_stack_and_queue/stack.md @@ -1092,7 +1092,7 @@ comments: true fun pop(): Int? { val num = peek() stackPeek = stackPeek?.next - stkSize--; + stkSize-- return num } diff --git a/zh-Hant/docs/chapter_tree/array_representation_of_tree.md b/zh-Hant/docs/chapter_tree/array_representation_of_tree.md index acb158e5e..ca0645fb7 100644 --- a/zh-Hant/docs/chapter_tree/array_representation_of_tree.md +++ b/zh-Hant/docs/chapter_tree/array_representation_of_tree.md @@ -125,7 +125,7 @@ comments: true ```kotlin title="" /* 二元樹的陣列表示 */ // 使用 null 來表示空位 - val tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 ) + val tree = arrayOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 ) ``` === "Ruby" @@ -1172,7 +1172,7 @@ comments: true === "Kotlin" ```kotlin title="array_binary_tree.kt" - /* 建構子 */ + /* 陣列表示下的二元樹類別 */ class ArrayBinaryTree(val tree: MutableList) { /* 串列容量 */ fun size(): Int { diff --git a/zh-Hant/docs/chapter_tree/binary_tree_traversal.md b/zh-Hant/docs/chapter_tree/binary_tree_traversal.md index dbd57e46f..e72af2ce6 100755 --- a/zh-Hant/docs/chapter_tree/binary_tree_traversal.md +++ b/zh-Hant/docs/chapter_tree/binary_tree_traversal.md @@ -305,7 +305,7 @@ comments: true val list = mutableListOf() while (queue.isNotEmpty()) { val node = queue.poll() // 隊列出隊 - list.add(node?._val!!) // 儲存節點值 + list.add(node?._val!!) // 儲存節點值 if (node.left != null) queue.offer(node.left) // 左子節點入列 if (node.right != null)