diff --git a/codes/kotlin/chapter_greedy/fractional_knapsack.kt b/codes/kotlin/chapter_greedy/fractional_knapsack.kt index 0b375b66c..d95bfa58c 100644 --- a/codes/kotlin/chapter_greedy/fractional_knapsack.kt +++ b/codes/kotlin/chapter_greedy/fractional_knapsack.kt @@ -6,34 +6,28 @@ package chapter_greedy -import java.util.* - /* 物品 */ class Item( val w: Int, // 物品 - val v: Int // 物品价值 + val v: Int // 物品价值 ) /* 分数背包:贪心 */ -fun fractionalKnapsack( - wgt: IntArray, - value: IntArray, - c: Int -): Double { +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], value[i]) + items[i] = Item(wgt[i], _val[i]) } // 按照单位价值 item.v / item.w 从高到低进行排序 - Arrays.sort(items, Comparator.comparingDouble { item: Item -> -(item.v.toDouble() / 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.toDouble() + res += item.v cap -= item.w } else { // 若剩余容量不足,则将当前物品的一部分装进背包 diff --git a/codes/kotlin/chapter_greedy/max_capacity.kt b/codes/kotlin/chapter_greedy/max_capacity.kt index ea9edfe27..9b4ffc7ef 100644 --- a/codes/kotlin/chapter_greedy/max_capacity.kt +++ b/codes/kotlin/chapter_greedy/max_capacity.kt @@ -19,8 +19,8 @@ fun maxCapacity(ht: IntArray): Int { // 循环贪心选择,直至两板相遇 while (i < j) { // 更新最大容量 - val cap = (min(ht[i].toDouble(), ht[j].toDouble()) * (j - i)).toInt() - res = max(res.toDouble(), cap.toDouble()).toInt() + val cap = min(ht[i], ht[j]) * (j - i) + res = max(res, cap) // 向内移动短板 if (ht[i] < ht[j]) { i++ diff --git a/codes/kotlin/chapter_greedy/max_product_cutting.kt b/codes/kotlin/chapter_greedy/max_product_cutting.kt index 9a24ce756..6945f8aac 100644 --- a/codes/kotlin/chapter_greedy/max_product_cutting.kt +++ b/codes/kotlin/chapter_greedy/max_product_cutting.kt @@ -19,14 +19,14 @@ fun maxProductCutting(n: Int): Int { val b = n % 3 if (b == 1) { // 当余数为 1 时,将一对 1 * 3 转化为 2 * 2 - return 3.0.pow((a - 1).toDouble()).toInt() * 2 * 2 + return 3.0.pow((a - 1)).toInt() * 2 * 2 } if (b == 2) { // 当余数为 2 时,不做处理 - return 3.0.pow(a.toDouble()).toInt() * 2 * 2 + return 3.0.pow(a).toInt() * 2 * 2 } // 当余数为 0 时,不做处理 - return 3.0.pow(a.toDouble()).toInt() + return 3.0.pow(a).toInt() } /* Driver Code */ diff --git a/codes/kotlin/chapter_hashing/array_hash_map.kt b/codes/kotlin/chapter_hashing/array_hash_map.kt index fcf0879f9..8f9f60928 100644 --- a/codes/kotlin/chapter_hashing/array_hash_map.kt +++ b/codes/kotlin/chapter_hashing/array_hash_map.kt @@ -14,15 +14,9 @@ class Pair( /* 基于数组实现的哈希表 */ class ArrayHashMap { + // 初始化数组,包含 100 个桶 private val buckets = arrayOfNulls(100) - init { - // 初始化数组,包含 100 个桶 - for (i in 0..<100) { - buckets[i] = null - } - } - /* 哈希函数 */ fun hashFunc(key: Int): Int { val index = key % 100 @@ -52,25 +46,27 @@ class ArrayHashMap { /* 获取所有键值对 */ fun pairSet(): MutableList { - val pairSet = ArrayList() + val pairSet = mutableListOf() for (pair in buckets) { - if (pair != null) pairSet.add(pair) + if (pair != null) + pairSet.add(pair) } return pairSet } /* 获取所有键 */ fun keySet(): MutableList { - val keySet = ArrayList() + val keySet = mutableListOf() for (pair in buckets) { - if (pair != null) keySet.add(pair.key) + if (pair != null) + keySet.add(pair.key) } return keySet } /* 获取所有值 */ fun valueSet(): MutableList { - val valueSet = ArrayList() + val valueSet = mutableListOf() for (pair in buckets) { pair?.let { valueSet.add(it.value) } } @@ -82,7 +78,7 @@ class ArrayHashMap { for (kv in pairSet()) { val key = kv.key val value = kv.value - println("${key}->${value}") + println("${key} -> ${value}") } } } @@ -104,7 +100,7 @@ fun main() { /* 查询操作 */ // 向哈希表中输入键 key ,得到值 value - val name: String? = map.get(15937) + val name = map.get(15937) println("\n输入学号 15937 ,查询到姓名 $name") /* 删除操作 */ @@ -114,7 +110,7 @@ fun main() { map.print() /* 遍历哈希表 */ - println("\n遍历键值对 Key->Value") + println("\n遍历键值对 Key -> Value") for (kv in map.pairSet()) { println("${kv.key} -> ${kv.value}") } @@ -126,4 +122,4 @@ fun main() { for (value in map.valueSet()) { println(value) } -} +} \ No newline at end of file diff --git a/codes/kotlin/chapter_hashing/built_in_hash.kt b/codes/kotlin/chapter_hashing/built_in_hash.kt index 948268800..88bb3ff67 100644 --- a/codes/kotlin/chapter_hashing/built_in_hash.kt +++ b/codes/kotlin/chapter_hashing/built_in_hash.kt @@ -11,15 +11,15 @@ import utils.ListNode /* Driver Code */ fun main() { val num = 3 - val hashNum = Integer.hashCode(num) + val hashNum = num.hashCode() println("整数 $num 的哈希值为 $hashNum") val bol = true - val hashBol = Boolean.hashCode() + val hashBol = bol.hashCode() println("布尔量 $bol 的哈希值为 $hashBol") val dec = 3.14159 - val hashDec = java.lang.Double.hashCode(dec) + val hashDec = dec.hashCode() println("小数 $dec 的哈希值为 $hashDec") val str = "Hello 算法" diff --git a/codes/kotlin/chapter_hashing/hash_map.kt b/codes/kotlin/chapter_hashing/hash_map.kt index ffc468fee..1a7ef1208 100644 --- a/codes/kotlin/chapter_hashing/hash_map.kt +++ b/codes/kotlin/chapter_hashing/hash_map.kt @@ -11,7 +11,7 @@ import utils.printHashMap /* Driver Code */ fun main() { /* 初始化哈希表 */ - val map: MutableMap = HashMap() + val map = HashMap() /* 添加操作 */ // 在哈希表中添加键值对 (key, value) diff --git a/codes/kotlin/chapter_hashing/hash_map_chaining.kt b/codes/kotlin/chapter_hashing/hash_map_chaining.kt index 205db6df5..d8558631b 100644 --- a/codes/kotlin/chapter_hashing/hash_map_chaining.kt +++ b/codes/kotlin/chapter_hashing/hash_map_chaining.kt @@ -20,7 +20,7 @@ class HashMapChaining() { capacity = 4 loadThres = 2.0 / 3.0 extendRatio = 2 - buckets = ArrayList(capacity) + buckets = mutableListOf() for (i in 0.. // 桶数组 - private val TOMBSTONE = Pair(-1, "-1") // 删除标记 + private var size: Int // 键值对数量 + private var capacity: Int // 哈希表容量 + private val loadThres: Double // 触发扩容的负载因子阈值 + private val extendRatio: Int // 扩容倍数 + private var buckets: Array // 桶数组 + private val TOMBSTONE: Pair // 删除标记 /* 构造方法 */ init { + size = 0 + capacity = 4 + loadThres = 2.0 / 3.0 + extendRatio = 2 buckets = arrayOfNulls(capacity) + TOMBSTONE = Pair(-1, "-1") } /* 哈希函数 */ diff --git a/codes/kotlin/chapter_hashing/simple_hash.kt b/codes/kotlin/chapter_hashing/simple_hash.kt index a4353361b..fdb51ef3c 100644 --- a/codes/kotlin/chapter_hashing/simple_hash.kt +++ b/codes/kotlin/chapter_hashing/simple_hash.kt @@ -6,7 +6,7 @@ package chapter_hashing -const val MODULUS = 10_0000_0007 +const val MODULUS = 1000000007 /* 加法哈希 */ fun addHash(key: String): Int { @@ -48,7 +48,7 @@ fun rotHash(key: String): Int { fun main() { val key = "Hello 算法" - var hash: Int = addHash(key) + var hash = addHash(key) println("加法哈希值为 $hash") hash = mulHash(key) diff --git a/codes/kotlin/chapter_heap/heap.kt b/codes/kotlin/chapter_heap/heap.kt index 08eef31be..1e0365c1f 100644 --- a/codes/kotlin/chapter_heap/heap.kt +++ b/codes/kotlin/chapter_heap/heap.kt @@ -25,7 +25,7 @@ fun testPop(heap: Queue) { fun main() { /* 初始化堆 */ // 初始化小顶堆 - val minHeap: PriorityQueue + var minHeap = PriorityQueue() // 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可) val maxHeap = PriorityQueue { a: Int, b: Int -> b - a } diff --git a/codes/kotlin/chapter_heap/my_heap.kt b/codes/kotlin/chapter_heap/my_heap.kt index 9d0ad0c73..1a63ed8d1 100644 --- a/codes/kotlin/chapter_heap/my_heap.kt +++ b/codes/kotlin/chapter_heap/my_heap.kt @@ -10,13 +10,14 @@ import utils.printHeap import java.util.* /* 大顶堆 */ -class MaxHeap(nums: List?) { +class MaxHeap(nums: MutableList?) { // 使用列表而非数组,这样无须考虑扩容问题 - // 将列表元素原封不动添加进堆 - private val maxHeap = ArrayList(nums!!) + private val maxHeap = mutableListOf() - /* 构造函数,根据输入列表建堆 */ + /* 构造方法,根据输入列表建堆 */ init { + // 将列表元素原封不动添加进堆 + maxHeap.addAll(nums!!) // 堆化除叶节点以外的其他所有节点 for (i in parent(size() - 1) downTo 0) { siftDown(i) diff --git a/codes/kotlin/chapter_searching/hashing_search.kt b/codes/kotlin/chapter_searching/hashing_search.kt index cbc0d9fbb..872c98985 100644 --- a/codes/kotlin/chapter_searching/hashing_search.kt +++ b/codes/kotlin/chapter_searching/hashing_search.kt @@ -7,7 +7,6 @@ package chapter_searching import utils.ListNode -import java.util.HashMap /* 哈希查找(数组) */ fun hashingSearchArray(map: Map, target: Int): Int {