From bfd0b3598dae1a3eea621e8748992c6a16e50727 Mon Sep 17 00:00:00 2001 From: krahets Date: Thu, 11 Apr 2024 17:17:43 +0800 Subject: [PATCH] build --- .../fractional_knapsack_problem.md | 27 ------- docs/chapter_hashing/hash_map.md | 71 ------------------- docs/chapter_sorting/quick_sort.md | 26 ++++--- en/docs/chapter_hashing/hash_map.md | 71 ------------------- .../fractional_knapsack_problem.md | 27 ------- zh-Hant/docs/chapter_hashing/hash_map.md | 71 ------------------- zh-Hant/docs/chapter_sorting/quick_sort.md | 26 ++++--- 7 files changed, 34 insertions(+), 285 deletions(-) diff --git a/docs/chapter_greedy/fractional_knapsack_problem.md b/docs/chapter_greedy/fractional_knapsack_problem.md index 039371add..4dfa81560 100644 --- a/docs/chapter_greedy/fractional_knapsack_problem.md +++ b/docs/chapter_greedy/fractional_knapsack_problem.md @@ -465,33 +465,6 @@ comments: true val v: Int // 物品价值 ) - /* 分数背包:贪心 */ - fun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double { - // 创建物品列表,包含两个属性:重量、价值 - var cap = c - val items = arrayOfNulls(wgt.size) - for (i in wgt.indices) { - items[i] = Item(wgt[i], _val[i]) - } - // 按照单位价值 item.v / item.w 从高到低进行排序 - items.sortBy { item: Item? -> -(item!!.v.toDouble() / item.w) } - // 循环贪心选择 - var res = 0.0 - for (item in items) { - if (item!!.w <= cap) { - // 若剩余容量充足,则将当前物品整个装进背包 - res += item.v - cap -= item.w - } else { - // 若剩余容量不足,则将当前物品的一部分装进背包 - res += item.v.toDouble() / item.w * cap - // 已无剩余容量,因此跳出循环 - break - } - } - return res - } - /* 分数背包:贪心 */ fun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double { // 创建物品列表,包含两个属性:重量、价值 diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 927103289..e65eeefce 100755 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -1587,77 +1587,6 @@ index = hash(key) % capacity var _val: String ) - /* 基于数组实现的哈希表 */ - class ArrayHashMap { - // 初始化数组,包含 100 个桶 - private val buckets = arrayOfNulls(100) - - /* 哈希函数 */ - fun hashFunc(key: Int): Int { - val index = key % 100 - return index - } - - /* 查询操作 */ - fun get(key: Int): String? { - val index = hashFunc(key) - val pair = buckets[index] ?: return null - return pair._val - } - - /* 添加操作 */ - fun put(key: Int, _val: String) { - val pair = Pair(key, _val) - val index = hashFunc(key) - buckets[index] = pair - } - - /* 删除操作 */ - fun remove(key: Int) { - val index = hashFunc(key) - // 置为 null ,代表删除 - buckets[index] = null - } - - /* 获取所有键值对 */ - fun pairSet(): MutableList { - val pairSet = mutableListOf() - for (pair in buckets) { - if (pair != null) - pairSet.add(pair) - } - return pairSet - } - - /* 获取所有键 */ - fun keySet(): MutableList { - val keySet = mutableListOf() - for (pair in buckets) { - if (pair != null) - keySet.add(pair.key) - } - return keySet - } - - /* 获取所有值 */ - fun valueSet(): MutableList { - val valueSet = mutableListOf() - for (pair in buckets) { - pair?.let { valueSet.add(it._val) } - } - return valueSet - } - - /* 打印哈希表 */ - fun print() { - for (kv in pairSet()) { - val key = kv.key - val _val = kv._val - println("${key} -> ${_val}") - } - } - } - /* 基于数组实现的哈希表 */ class ArrayHashMap { // 初始化数组,包含 100 个桶 diff --git a/docs/chapter_sorting/quick_sort.md b/docs/chapter_sorting/quick_sort.md index 33a75c0cf..b0fb8f4db 100755 --- a/docs/chapter_sorting/quick_sort.md +++ b/docs/chapter_sorting/quick_sort.md @@ -1372,15 +1372,23 @@ comments: true === "Kotlin" ```kotlin title="quick_sort.kt" - /* 快速排序 */ - fun quickSort(nums: IntArray, left: Int, right: Int) { - // 子数组长度为 1 时终止递归 - if (left >= right) return - // 哨兵划分 - val pivot = partition(nums, left, right) - // 递归左子数组、右子数组 - quickSort(nums, left, pivot - 1) - quickSort(nums, pivot + 1, right) + /* 快速排序(尾递归优化) */ + fun quickSortTailCall(nums: IntArray, left: Int, right: Int) { + // 子数组长度为 1 时终止 + var l = left + var r = right + while (l < r) { + // 哨兵划分操作 + val pivot = partition(nums, l, r) + // 对两个子数组中较短的那个执行快速排序 + if (pivot - l < r - pivot) { + quickSort(nums, l, pivot - 1) // 递归排序左子数组 + l = pivot + 1 // 剩余未排序区间为 [pivot + 1, right] + } else { + quickSort(nums, pivot + 1, r) // 递归排序右子数组 + r = pivot - 1 // 剩余未排序区间为 [left, pivot - 1] + } + } } ``` diff --git a/en/docs/chapter_hashing/hash_map.md b/en/docs/chapter_hashing/hash_map.md index 8ab6f96dd..922eb15c2 100755 --- a/en/docs/chapter_hashing/hash_map.md +++ b/en/docs/chapter_hashing/hash_map.md @@ -1546,77 +1546,6 @@ The following code implements a simple hash table. Here, we encapsulate `key` an var _val: String ) - /* 基于数组实现的哈希表 */ - class ArrayHashMap { - // 初始化数组,包含 100 个桶 - private val buckets = arrayOfNulls(100) - - /* 哈希函数 */ - fun hashFunc(key: Int): Int { - val index = key % 100 - return index - } - - /* 查询操作 */ - fun get(key: Int): String? { - val index = hashFunc(key) - val pair = buckets[index] ?: return null - return pair._val - } - - /* 添加操作 */ - fun put(key: Int, _val: String) { - val pair = Pair(key, _val) - val index = hashFunc(key) - buckets[index] = pair - } - - /* 删除操作 */ - fun remove(key: Int) { - val index = hashFunc(key) - // 置为 null ,代表删除 - buckets[index] = null - } - - /* 获取所有键值对 */ - fun pairSet(): MutableList { - val pairSet = mutableListOf() - for (pair in buckets) { - if (pair != null) - pairSet.add(pair) - } - return pairSet - } - - /* 获取所有键 */ - fun keySet(): MutableList { - val keySet = mutableListOf() - for (pair in buckets) { - if (pair != null) - keySet.add(pair.key) - } - return keySet - } - - /* 获取所有值 */ - fun valueSet(): MutableList { - val valueSet = mutableListOf() - for (pair in buckets) { - pair?.let { valueSet.add(it._val) } - } - return valueSet - } - - /* 打印哈希表 */ - fun print() { - for (kv in pairSet()) { - val key = kv.key - val _val = kv._val - println("${key} -> ${_val}") - } - } - } - /* 基于数组实现的哈希表 */ class ArrayHashMap { // 初始化数组,包含 100 个桶 diff --git a/zh-Hant/docs/chapter_greedy/fractional_knapsack_problem.md b/zh-Hant/docs/chapter_greedy/fractional_knapsack_problem.md index 85d0b8306..9ed646117 100644 --- a/zh-Hant/docs/chapter_greedy/fractional_knapsack_problem.md +++ b/zh-Hant/docs/chapter_greedy/fractional_knapsack_problem.md @@ -465,33 +465,6 @@ comments: true val v: Int // 物品價值 ) - /* 分數背包:貪婪 */ - fun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double { - // 建立物品串列,包含兩個屬性:重量、價值 - var cap = c - val items = arrayOfNulls(wgt.size) - for (i in wgt.indices) { - items[i] = Item(wgt[i], _val[i]) - } - // 按照單位價值 item.v / item.w 從高到低進行排序 - items.sortBy { item: Item? -> -(item!!.v.toDouble() / item.w) } - // 迴圈貪婪選擇 - var res = 0.0 - for (item in items) { - if (item!!.w <= cap) { - // 若剩餘容量充足,則將當前物品整個裝進背包 - res += item.v - cap -= item.w - } else { - // 若剩餘容量不足,則將當前物品的一部分裝進背包 - res += item.v.toDouble() / item.w * cap - // 已無剩餘容量,因此跳出迴圈 - break - } - } - return res - } - /* 分數背包:貪婪 */ fun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double { // 建立物品串列,包含兩個屬性:重量、價值 diff --git a/zh-Hant/docs/chapter_hashing/hash_map.md b/zh-Hant/docs/chapter_hashing/hash_map.md index 74c49dc9e..77c69317f 100755 --- a/zh-Hant/docs/chapter_hashing/hash_map.md +++ b/zh-Hant/docs/chapter_hashing/hash_map.md @@ -1587,77 +1587,6 @@ index = hash(key) % capacity var _val: String ) - /* 基於陣列實現的雜湊表 */ - class ArrayHashMap { - // 初始化陣列,包含 100 個桶 - private val buckets = arrayOfNulls(100) - - /* 雜湊函式 */ - fun hashFunc(key: Int): Int { - val index = key % 100 - return index - } - - /* 查詢操作 */ - fun get(key: Int): String? { - val index = hashFunc(key) - val pair = buckets[index] ?: return null - return pair._val - } - - /* 新增操作 */ - fun put(key: Int, _val: String) { - val pair = Pair(key, _val) - val index = hashFunc(key) - buckets[index] = pair - } - - /* 刪除操作 */ - fun remove(key: Int) { - val index = hashFunc(key) - // 置為 null ,代表刪除 - buckets[index] = null - } - - /* 獲取所有鍵值對 */ - fun pairSet(): MutableList { - val pairSet = mutableListOf() - for (pair in buckets) { - if (pair != null) - pairSet.add(pair) - } - return pairSet - } - - /* 獲取所有鍵 */ - fun keySet(): MutableList { - val keySet = mutableListOf() - for (pair in buckets) { - if (pair != null) - keySet.add(pair.key) - } - return keySet - } - - /* 獲取所有值 */ - fun valueSet(): MutableList { - val valueSet = mutableListOf() - for (pair in buckets) { - pair?.let { valueSet.add(it._val) } - } - return valueSet - } - - /* 列印雜湊表 */ - fun print() { - for (kv in pairSet()) { - val key = kv.key - val _val = kv._val - println("${key} -> ${_val}") - } - } - } - /* 基於陣列實現的雜湊表 */ class ArrayHashMap { // 初始化陣列,包含 100 個桶 diff --git a/zh-Hant/docs/chapter_sorting/quick_sort.md b/zh-Hant/docs/chapter_sorting/quick_sort.md index c137f4962..adf26b26d 100755 --- a/zh-Hant/docs/chapter_sorting/quick_sort.md +++ b/zh-Hant/docs/chapter_sorting/quick_sort.md @@ -1372,15 +1372,23 @@ comments: true === "Kotlin" ```kotlin title="quick_sort.kt" - /* 快速排序 */ - fun quickSort(nums: IntArray, left: Int, right: Int) { - // 子陣列長度為 1 時終止遞迴 - if (left >= right) return - // 哨兵劃分 - val pivot = partition(nums, left, right) - // 遞迴左子陣列、右子陣列 - quickSort(nums, left, pivot - 1) - quickSort(nums, pivot + 1, right) + /* 快速排序(尾遞迴最佳化) */ + fun quickSortTailCall(nums: IntArray, left: Int, right: Int) { + // 子陣列長度為 1 時終止 + var l = left + var r = right + while (l < r) { + // 哨兵劃分操作 + val pivot = partition(nums, l, r) + // 對兩個子陣列中較短的那個執行快速排序 + if (pivot - l < r - pivot) { + quickSort(nums, l, pivot - 1) // 遞迴排序左子陣列 + l = pivot + 1 // 剩餘未排序區間為 [pivot + 1, right] + } else { + quickSort(nums, pivot + 1, r) // 遞迴排序右子陣列 + r = pivot - 1 // 剩餘未排序區間為 [left, pivot - 1] + } + } } ```