mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-22 08:17:15 +00:00
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:
@@ -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 }
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user