Improve readability of Kotlin code (#1236)

* style(kotlin): Improve kotlin codes readability.

* remove redundant quotes.

* style(kotlin): improve codes readability.
This commit is contained in:
curtishd
2024-04-08 16:09:34 +08:00
committed by GitHub
parent ba1df4a3e8
commit 3fe8f67ba9
12 changed files with 46 additions and 51 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ fun testPop(heap: Queue<Int>) {
fun main() {
/* 初始化堆 */
// 初始化小顶堆
val minHeap: PriorityQueue<Int>
var minHeap = PriorityQueue<Int>()
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
val maxHeap = PriorityQueue { a: Int, b: Int -> b - a }
+5 -4
View File
@@ -10,13 +10,14 @@ import utils.printHeap
import java.util.*
/* 大顶堆 */
class MaxHeap(nums: List<Int>?) {
class MaxHeap(nums: MutableList<Int>?) {
// 使用列表而非数组,这样无须考虑扩容问题
// 将列表元素原封不动添加进堆
private val maxHeap = ArrayList(nums!!)
private val maxHeap = mutableListOf<Int>()
/* 构造函数,根据输入列表建堆 */
/* 构造方法,根据输入列表建堆 */
init {
// 将列表元素原封不动添加进堆
maxHeap.addAll(nums!!)
// 堆化除叶节点以外的其他所有节点
for (i in parent(size() - 1) downTo 0) {
siftDown(i)