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
@@ -60,9 +60,9 @@ fun linearRecur(n: Int) {
/* 平方阶 */
fun quadratic(n: Int) {
// 矩阵占用 O(n^2) 空间
val numMatrix: Array<Array<Int>?> = arrayOfNulls(n)
val numMatrix = arrayOfNulls<Array<Int>?>(n)
// 二维列表占用 O(n^2) 空间
val numList: MutableList<MutableList<Int>> = arrayListOf()
val numList = mutableListOf<MutableList<Int>>()
for (i in 0..<n) {
val tmp = mutableListOf<Int>()
for (j in 0..<n) {
@@ -104,6 +104,6 @@ fun main() {
quadratic(n)
quadraticRecur(n)
// 指数阶
val root: TreeNode? = buildTree(n)
val root = buildTree(n)
printTree(root)
}
@@ -9,7 +9,7 @@ package chapter_computational_complexity.time_complexity
/* 常数阶 */
fun constant(n: Int): Int {
var count = 0
val size = 10_0000
val size = 100000
for (i in 0..<size)
count++
return count
@@ -48,7 +48,7 @@ fun quadratic(n: Int): Int {
/* 平方阶(冒泡排序) */
fun bubbleSort(nums: IntArray): Int {
var count = 0
var count = 0 // 计数器
// 外循环:未排序区间为 [0, i]
for (i in nums.size - 1 downTo 1) {
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
@@ -109,7 +109,7 @@ fun linearLogRecur(n: Int): Int {
if (n <= 1)
return 1
var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)
for (i in 0..<n.toInt()) {
for (i in 0..<n) {
count++
}
return count
@@ -133,7 +133,7 @@ fun main() {
val n = 8
println("输入数据大小 n = $n")
var count: Int = constant(n)
var count = constant(n)
println("常数阶的操作数量 = $count")
count = linear(n)
@@ -144,7 +144,8 @@ fun main() {
count = quadratic(n)
println("平方阶的操作数量 = $count")
val nums = IntArray(n)
for (i in 0..<n) nums[i] = n - i // [n,n-1,...,2,1]
for (i in 0..<n)
nums[i] = n - i // [n,n-1,...,2,1]
count = bubbleSort(nums)
println("平方阶(冒泡排序)的操作数量 = $count")
@@ -13,10 +13,9 @@ fun randomNumbers(n: Int): Array<Int?> {
for (i in 0..<n) {
nums[i] = i + 1
}
// 随机打乱数组元素
val mutableList = nums.toMutableList()
// 随机打乱数组元素
mutableList.shuffle()
// Integer[] -> int[]
val res = arrayOfNulls<Int>(n)
for (i in 0..<n) {
res[i] = mutableList[i]
@@ -39,8 +38,8 @@ fun findOne(nums: Array<Int?>): Int {
fun main() {
for (i in 0..9) {
val n = 100
val nums: Array<Int?> = randomNumbers(n)
val index: Int = findOne(nums)
val nums = randomNumbers(n)
val index = findOne(nums)
println("\n数组 [ 1, 2, ..., n ] 被打乱后 = ${nums.contentToString()}")
println("数字 1 的索引为 $index")
}