mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-14 20:50:58 +00:00
Many bug fixes and improvements (#1270)
* Add Ruby and Kotlin icons Add the avatar of @curtishd * Update README * Synchronize zh-hant and zh versions. * Translate the pythontutor blocks to traditional Chinese * Fix en/mkdocs.yml * Update the landing page of the en version. * Fix the Dockerfile * Refine the en landingpage * Fix en landing page * Reset the README.md
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
package utils
|
||||
|
||||
/* 鏈結串列節點 */
|
||||
class ListNode(var value: Int) {
|
||||
class ListNode(var _val: Int) {
|
||||
var next: ListNode? = null
|
||||
|
||||
companion object {
|
||||
@@ -15,11 +15,11 @@ class ListNode(var value: Int) {
|
||||
fun arrToLinkedList(arr: IntArray): ListNode? {
|
||||
val dum = ListNode(0)
|
||||
var head = dum
|
||||
for (value in arr) {
|
||||
head.next = ListNode(value)
|
||||
for (_val in arr) {
|
||||
head.next = ListNode(_val)
|
||||
head = head.next!!
|
||||
}
|
||||
return dum.next
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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,9 +31,9 @@ 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())
|
||||
list.add(head._val.toString())
|
||||
head = head.next
|
||||
}
|
||||
println(list.joinToString(separator = " -> "))
|
||||
@@ -70,7 +70,7 @@ fun printTree(root: TreeNode?, prev: Trunk?, isRight: Boolean) {
|
||||
}
|
||||
|
||||
showTrunks(trunk)
|
||||
println(" ${root.value}")
|
||||
println(" ${root._val}")
|
||||
|
||||
if (prev != null) {
|
||||
prev.str = prevStr
|
||||
@@ -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,8 +7,9 @@
|
||||
package utils
|
||||
|
||||
/* 二元樹節點類別 */
|
||||
/* 建構子 */
|
||||
class TreeNode(
|
||||
var value: Int // 節點值
|
||||
var _val: Int // 節點值
|
||||
) {
|
||||
var height: Int = 0 // 節點高度
|
||||
var left: TreeNode? = null // 左子節點引用
|
||||
@@ -53,14 +54,14 @@ class TreeNode(
|
||||
while (i >= res.size) {
|
||||
res.add(null)
|
||||
}
|
||||
res[i] = root.value
|
||||
res[i] = root._val
|
||||
treeToListDFS(root.left, 2 * i + 1, res)
|
||||
treeToListDFS(root.right, 2 * i + 2, res)
|
||||
}
|
||||
|
||||
/* 將二元樹序列化為串列 */
|
||||
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
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
package utils
|
||||
|
||||
/* 頂點類別 */
|
||||
class Vertex(val value: Int) {
|
||||
class Vertex(val _val: Int) {
|
||||
companion object {
|
||||
/* 輸入值串列 vals ,返回頂點串列 vets */
|
||||
fun valsToVets(vals: IntArray): Array<Vertex?> {
|
||||
@@ -19,10 +19,10 @@ 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)
|
||||
vals.add(vet!!._val)
|
||||
}
|
||||
return vals
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user