mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
Refine kotlin code (#1241)
* style(kotlin): Improve kotlin codes readability. * remove redundant quotes. * style(kotlin): improve codes readability. * style(kotlin): refine kotlin codes. * Create kotlin.yml * Create kotlin.yml * Delete .github/workflows/kotlin * Delete .github/workflows/main.yml * Create kotlin.yml * Update kotlin.yml * Delete .github/workflows/kotlin.yml * Create hello_world_workflow.main.kts * Delete .github/workflows/hello_world_workflow.main.kts * remove empty line
This commit is contained in:
@@ -20,7 +20,7 @@ fun <T> printMatrix(matrix: Array<Array<T>>) {
|
||||
}
|
||||
|
||||
/* 打印矩阵(List) */
|
||||
fun <T> printMatrix(matrix: List<List<T>>) {
|
||||
fun <T> printMatrix(matrix: MutableList<MutableList<T>>) {
|
||||
println("[")
|
||||
for (row in matrix) {
|
||||
println(" $row,")
|
||||
@@ -31,7 +31,7 @@ fun <T> printMatrix(matrix: List<List<T>>) {
|
||||
/* 打印链表 */
|
||||
fun printLinkedList(h: ListNode?) {
|
||||
var head = h
|
||||
val list = ArrayList<String>()
|
||||
val list = mutableListOf<String>()
|
||||
while (head != null) {
|
||||
list.add(head.value.toString())
|
||||
head = head.next
|
||||
@@ -91,16 +91,17 @@ fun showTrunks(p: Trunk?) {
|
||||
/* 打印哈希表 */
|
||||
fun <K, V> printHashMap(map: Map<K, V>) {
|
||||
for ((key, value) in map) {
|
||||
println(key.toString() + " -> " + value)
|
||||
println("${key.toString()} -> $value")
|
||||
}
|
||||
}
|
||||
|
||||
/* 打印堆 */
|
||||
fun printHeap(queue: Queue<Int>?) {
|
||||
val list = queue?.let { ArrayList(it) }
|
||||
val list = mutableListOf<Int?>()
|
||||
queue?.let { list.addAll(it) }
|
||||
print("堆的数组表示:")
|
||||
println(list)
|
||||
println("堆的树状表示:")
|
||||
val root = list?.let { TreeNode.listToTree(it) }
|
||||
val root = TreeNode.listToTree(list)
|
||||
printTree(root)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
package utils
|
||||
|
||||
/* 二叉树节点类 */
|
||||
/* 构造方法 */
|
||||
class TreeNode(
|
||||
var value: Int // 节点值
|
||||
) {
|
||||
@@ -59,8 +60,8 @@ class TreeNode(
|
||||
}
|
||||
|
||||
/* 将二叉树序列化为列表 */
|
||||
fun treeToList(root: TreeNode?): List<Int?> {
|
||||
val res = ArrayList<Int?>()
|
||||
fun treeToList(root: TreeNode?): MutableList<Int?> {
|
||||
val res = mutableListOf<Int?>()
|
||||
treeToListDFS(root, 0, res)
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ class Vertex(val value: Int) {
|
||||
}
|
||||
|
||||
/* 输入顶点列表 vets ,返回值列表 vals */
|
||||
fun vetsToVals(vets: List<Vertex?>): List<Int> {
|
||||
val vals = ArrayList<Int>()
|
||||
fun vetsToVals(vets: MutableList<Vertex?>): MutableList<Int> {
|
||||
val vals = mutableListOf<Int>()
|
||||
for (vet in vets) {
|
||||
vals.add(vet!!.value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user