Skip to content

Commit

Permalink
Add kotlin code block for chapter_computational_complexity (#1187)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
curtishd authored Mar 28, 2024
1 parent 16350b6 commit 2f505e7
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/chapter_array_and_linkedlist/linked_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
/* 链表节点类 */
// 构造方法
class ListNode(x: Int) {
val `val`: Int = x // 节点值
val _val: Int = x // 节点值
val next: ListNode? = null // 指向下一个节点的引用
}
```
Expand Down Expand Up @@ -662,7 +662,7 @@
/* 双向链表节点类 */
// 构造方法
class ListNode(x: Int) {
val `val`: Int = x // 节点值
val _val: Int = x // 节点值
val next: ListNode? = null // 指向后继节点的引用
val prev: ListNode? = null // 指向前驱节点的引用
}
Expand Down
41 changes: 39 additions & 2 deletions docs/chapter_computational_complexity/space_complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,24 @@
=== "Kotlin"

```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"
Expand Down Expand Up @@ -464,7 +481,13 @@
=== "Kotlin"

```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"
Expand Down Expand Up @@ -708,7 +731,21 @@
=== "Kotlin"

```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"
Expand Down
53 changes: 49 additions & 4 deletions docs/chapter_computational_complexity/time_complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,16 @@
=== "Kotlin"

```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"
Expand Down Expand Up @@ -438,7 +447,22 @@ $$
=== "Kotlin"

```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"
Expand Down Expand Up @@ -638,7 +662,15 @@ $$
=== "Kotlin"

```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"
Expand Down Expand Up @@ -901,7 +933,20 @@ $T(n)$ 是一次函数,说明其运行时间的增长趋势是线性的,因
=== "Kotlin"

```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"
Expand Down

0 comments on commit 2f505e7

Please sign in to comment.