From 2f505e7f3895b79388a7d47e2aa6ee2081dec732 Mon Sep 17 00:00:00 2001 From: curtishd <131777542+curtishd@users.noreply.github.com> Date: Thu, 28 Mar 2024 17:08:39 +0800 Subject: [PATCH] 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. --- .../linked_list.md | 4 +- .../space_complexity.md | 41 +++++++++++++- .../time_complexity.md | 53 +++++++++++++++++-- 3 files changed, 90 insertions(+), 8 deletions(-) diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index a7f16354..4b2053dc 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -165,7 +165,7 @@ /* 链表节点类 */ // 构造方法 class ListNode(x: Int) { - val `val`: Int = x // 节点值 + val _val: Int = x // 节点值 val next: ListNode? = null // 指向下一个节点的引用 } ``` @@ -662,7 +662,7 @@ /* 双向链表节点类 */ // 构造方法 class ListNode(x: Int) { - val `val`: Int = x // 节点值 + val _val: Int = x // 节点值 val next: ListNode? = null // 指向后继节点的引用 val prev: ListNode? = null // 指向前驱节点的引用 } diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index 20788114..ab03ebcb 100755 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -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" @@ -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" @@ -708,7 +731,21 @@ === "Kotlin" ```kotlin title="" - + fun function(): Int { + // 执行某些操作 + return 0 + } + /* 循环 O(1) */ + fun loop(n: Int) { + for (i in 0..