Improve readability of kotlin code (#1233)

* style(kotlin): Improve kotlin codes readability.

* remove redundant quotes.
This commit is contained in:
curtishd
2024-04-07 18:56:59 +08:00
committed by GitHub
parent e121665772
commit 5725b8a0f1
15 changed files with 85 additions and 101 deletions
@@ -10,7 +10,13 @@ import utils.TreeNode
import utils.printTree
/* 构建二叉树:分治 */
fun dfs(preorder: IntArray, inorderMap: Map<Int?, Int?>, i: Int, l: Int, r: Int): TreeNode? {
fun dfs(
preorder: IntArray,
inorderMap: Map<Int?, Int?>,
i: Int,
l: Int,
r: Int
): TreeNode? {
// 子树区间为空时终止
if (r - l < 0) return null
// 初始化根节点
@@ -28,7 +34,7 @@ fun dfs(preorder: IntArray, inorderMap: Map<Int?, Int?>, i: Int, l: Int, r: Int)
/* 构建二叉树 */
fun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {
// 初始化哈希表,存储 inorder 元素到索引的映射
val inorderMap: MutableMap<Int?, Int?> = HashMap()
val inorderMap = HashMap<Int?, Int?>()
for (i in inorder.indices) {
inorderMap[inorder[i]] = i
}
@@ -40,8 +46,8 @@ fun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {
fun main() {
val preorder = intArrayOf(3, 9, 2, 1, 7)
val inorder = intArrayOf(9, 3, 1, 2, 7)
println("前序遍历 = " + preorder.contentToString())
println("中序遍历 = " + inorder.contentToString())
println("前序遍历 = ${preorder.contentToString()}")
println("中序遍历 = ${inorder.contentToString()}")
val root = buildTree(preorder, inorder)
println("构建的二叉树为:")
@@ -9,7 +9,7 @@ package chapter_divide_and_conquer.hanota
/* 移动一个圆盘 */
fun move(src: MutableList<Int>, tar: MutableList<Int>) {
// 从 src 顶部拿出一个圆盘
val pan: Int = src.removeAt(src.size - 1)
val pan = src.removeAt(src.size - 1)
// 将圆盘放入 tar 顶部
tar.add(pan)
}
@@ -39,9 +39,9 @@ fun solveHanota(A: MutableList<Int>, B: MutableList<Int>, C: MutableList<Int>) {
/* Driver Code */
fun main() {
// 列表尾部是柱子顶部
val A: MutableList<Int> = ArrayList(mutableListOf(5, 4, 3, 2, 1))
val B: MutableList<Int> = ArrayList()
val C: MutableList<Int> = ArrayList()
val A = mutableListOf(5, 4, 3, 2, 1)
val B = mutableListOf<Int>()
val C = mutableListOf<Int>()
println("初始状态下:")
println("A = $A")
println("B = $B")
@@ -53,4 +53,4 @@ fun main() {
println("A = $A")
println("B = $B")
println("C = $C")
}
}