Add kotlin code block for chapter_computational_complexity (#1187)

* Add kotlin code block for array.md and backtracking_algorithm.md.

* add kotlin code block for chapter_computational_complexity.

* Update space_complexity.md

* preview linked_list.md

* Update linked_list.md

* fill in the missing code blocks.
This commit is contained in:
curtishd 2024-03-28 17:08:39 +08:00 committed by GitHub
parent 16350b65e4
commit 2f505e7f38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 90 additions and 8 deletions

View file

@ -165,7 +165,7 @@
/* 链表节点类 */ /* 链表节点类 */
// 构造方法 // 构造方法
class ListNode(x: Int) { class ListNode(x: Int) {
val `val`: Int = x // 节点值 val _val: Int = x // 节点值
val next: ListNode? = null // 指向下一个节点的引用 val next: ListNode? = null // 指向下一个节点的引用
} }
``` ```
@ -662,7 +662,7 @@
/* 双向链表节点类 */ /* 双向链表节点类 */
// 构造方法 // 构造方法
class ListNode(x: Int) { class ListNode(x: Int) {
val `val`: Int = x // 节点值 val _val: Int = x // 节点值
val next: ListNode? = null // 指向后继节点的引用 val next: ListNode? = null // 指向后继节点的引用
val prev: ListNode? = null // 指向前驱节点的引用 val prev: ListNode? = null // 指向前驱节点的引用
} }

View file

@ -312,7 +312,24 @@
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
/* 类 */
class Node(var _val: Int) {
var next: Node? = null
}
/* 函数 */
fun function(): Int {
// 执行某些操作...
return 0
}
fun algorithm(n: Int): Int { // 输入数据
val a = 0 // 暂存数据(常量)
var b = 0 // 暂存数据(变量)
val node = Node(0) // 暂存数据(对象)
val c = function() // 栈帧空间(调用函数)
return a + b + c // 输出数据
}
``` ```
=== "Zig" === "Zig"
@ -464,7 +481,13 @@
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
fun algorithm(n: Int) {
val a = 0 // O(1)
val b = IntArray(10000) // O(1)
if (n > 10) {
val nums = IntArray(n) // O(n)
}
}
``` ```
=== "Zig" === "Zig"
@ -708,7 +731,21 @@
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
fun function(): Int {
// 执行某些操作
return 0
}
/* 循环 O(1) */
fun loop(n: Int) {
for (i in 0..<n) {
function()
}
}
/* 递归 O(n) */
fun recur(n: Int) {
if (n == 1) return
return recur(n - 1)
}
``` ```
=== "Zig" === "Zig"

View file

@ -174,7 +174,16 @@
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
// 在某运行平台下
fun algorithm(n: Int) {
var a = 2 // 1 ns
a = a + 1 // 1 ns
a = a * 2 // 10 ns
// 循环 n 次
for (i in 0..<n) { // 1 ns 每轮都要执行 i++
println(0) // 5 ns
}
}
``` ```
=== "Zig" === "Zig"
@ -438,7 +447,22 @@ $$
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
// 算法 A 的时间复杂度:常数阶
fun algoritm_A(n: Int) {
println(0)
}
// 算法 B 的时间复杂度:线性阶
fun algorithm_B(n: Int) {
for (i in 0..<n){
println(0)
}
}
// 算法 C 的时间复杂度:常数阶
fun algorithm_C(n: Int) {
for (i in 0..<1000000) {
println(0)
}
}
``` ```
=== "Zig" === "Zig"
@ -638,7 +662,15 @@ $$
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
fun algorithm(n: Int) {
var a = 1 // +1
a = a + 1 // +1
a = a * 2 // +1
// 循环 n 次
for (i in 0..<n) { // +1每轮都执行 i ++
println(0) // +1
}
}
``` ```
=== "Zig" === "Zig"
@ -901,7 +933,20 @@ $T(n)$ 是一次函数,说明其运行时间的增长趋势是线性的,因
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
fun algorithm(n: Int) {
var a = 1 // +0技巧 1
a = a + n // +0技巧 1
// +n技巧 2
for (i in 0..<5 * n + 1) {
println(0)
}
// +n*n技巧 3
for (i in 0..<2 * n) {
for (j in 0..<n + 1) {
println(0)
}
}
}
``` ```
=== "Zig" === "Zig"