Simplify kotlin code and improve code readability (#1198)

* Add kotlin code block for chapter_hashing

* Add kotlin code block for chapter_heap.

* Add kotlin code block for chapter_stack_and_queue and chapter_tree

* fix indentation

* Update binary_tree.md

* style(kotlin): simplify code and improve readability.

* simplify kt code for chapter_computational_complexity.

* style(kotlin): replace ArrayList with MutableList.

* Update subset_sum_i.kt

Use kotlin api instead of java.

* Update subset_sum_ii.kt

use kotlin api instead of java

* style(kotlin): replace ArrayList with mutablelist.

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
curtishd
2024-04-07 01:31:58 +08:00
committed by GitHub
parent 931d8f5089
commit 2655a2f66a
17 changed files with 101 additions and 101 deletions
@@ -12,11 +12,11 @@ fun backtrack(
target: Int,
total: Int,
choices: IntArray,
res: MutableList<List<Int>?>
res: MutableList<MutableList<Int>?>
) {
// 子集和等于 target 时,记录解
if (total == target) {
res.add(ArrayList(state))
res.add(state.toMutableList())
return
}
// 遍历所有选择
@@ -35,10 +35,10 @@ fun backtrack(
}
/* 求解子集和 I(包含重复子集) */
fun subsetSumINaive(nums: IntArray, target: Int): List<List<Int>?> {
val state: MutableList<Int> = ArrayList() // 状态(子集)
fun subsetSumINaive(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {
val state = mutableListOf<Int>() // 状态(子集)
val total = 0 // 子集和
val res: MutableList<List<Int>?> = ArrayList() // 结果列表(子集列表)
val res = mutableListOf<MutableList<Int>?>() // 结果列表(子集列表)
backtrack(state, target, total, nums, res)
return res
}
@@ -47,8 +47,7 @@ fun subsetSumINaive(nums: IntArray, target: Int): List<List<Int>?> {
fun main() {
val nums = intArrayOf(3, 4, 5)
val target = 9
val res: List<List<Int>?> = subsetSumINaive(nums, target)
val res = subsetSumINaive(nums, target)
println("输入数组 nums = ${nums.contentToString()}, target = $target")
println("所有和等于 $target 的子集 res = $res")