From 306dc019ef3d2fd0b51af57c58cc337040adde3b Mon Sep 17 00:00:00 2001 From: curtishd <131777542+curtishd@users.noreply.github.com> Date: Tue, 27 Feb 2024 17:04:57 +0800 Subject: [PATCH] Add Kotlin code for computational complexity (#1090) * feat(kotlin):new kotlin support files * fix(kotlin): reviewed the formatting, comments and so on. * fix(kotlin): fix the indentation and format * feat(kotlin): Add kotlin code for the backtraking chapter. * fix(kotlin): fix incorrect output of preorder_traversal_iii_template.kt file * fix(kotlin): simplify kotlin codes * fix(kotlin): modify n_queens.kt for consistency. * feat(kotlin): add kotlin code for computational complexity. * fix(kotlin): remove iteration folder. * fix(kotlin): remove n_queens.kt file out of folder. * fix(kotlin): remove some folders. * style(kotlin): modified two chapters. --- .../{n_queens => }/n_queens.kt | 0 .../{permutations_i => }/permutations_i.kt | 0 .../{permutations_ii => }/permutations_ii.kt | 0 .../preorder_traversal_i_compact.kt | 0 .../preorder_traversal_ii_compact.kt | 0 .../preorder_traversal_iii_compact.kt | 0 .../preorder_traversal_iii_template.kt | 0 .../{subset_sum_i => }/subset_sum_i.kt | 0 .../subset_sum_i_naive.kt | 0 .../{subset_sum_ii => }/subset_sum_ii.kt | 0 .../iteration.kt | 74 ++++++++ .../recursion.kt | 76 ++++++++ .../space_complexity.kt | 109 ++++++++++++ .../time_complexity.kt | 166 ++++++++++++++++++ .../worst_best_time_complexity.kt | 47 +++++ 15 files changed, 472 insertions(+) rename codes/kotlin/chapter_backtracking/{n_queens => }/n_queens.kt (100%) rename codes/kotlin/chapter_backtracking/{permutations_i => }/permutations_i.kt (100%) rename codes/kotlin/chapter_backtracking/{permutations_ii => }/permutations_ii.kt (100%) rename codes/kotlin/chapter_backtracking/{preorder_traversal_i_compact => }/preorder_traversal_i_compact.kt (100%) rename codes/kotlin/chapter_backtracking/{preorder_traversal_ii_compact => }/preorder_traversal_ii_compact.kt (100%) rename codes/kotlin/chapter_backtracking/{preorder_traversal_iii_compact => }/preorder_traversal_iii_compact.kt (100%) rename codes/kotlin/chapter_backtracking/{preorder_traversal_iii_template => }/preorder_traversal_iii_template.kt (100%) rename codes/kotlin/chapter_backtracking/{subset_sum_i => }/subset_sum_i.kt (100%) rename codes/kotlin/chapter_backtracking/{subset_sum_i_naive => }/subset_sum_i_naive.kt (100%) rename codes/kotlin/chapter_backtracking/{subset_sum_ii => }/subset_sum_ii.kt (100%) create mode 100644 codes/kotlin/chapter_computational_complexity/iteration.kt create mode 100644 codes/kotlin/chapter_computational_complexity/recursion.kt create mode 100644 codes/kotlin/chapter_computational_complexity/space_complexity.kt create mode 100644 codes/kotlin/chapter_computational_complexity/time_complexity.kt create mode 100644 codes/kotlin/chapter_computational_complexity/worst_best_time_complexity.kt diff --git a/codes/kotlin/chapter_backtracking/n_queens/n_queens.kt b/codes/kotlin/chapter_backtracking/n_queens.kt similarity index 100% rename from codes/kotlin/chapter_backtracking/n_queens/n_queens.kt rename to codes/kotlin/chapter_backtracking/n_queens.kt diff --git a/codes/kotlin/chapter_backtracking/permutations_i/permutations_i.kt b/codes/kotlin/chapter_backtracking/permutations_i.kt similarity index 100% rename from codes/kotlin/chapter_backtracking/permutations_i/permutations_i.kt rename to codes/kotlin/chapter_backtracking/permutations_i.kt diff --git a/codes/kotlin/chapter_backtracking/permutations_ii/permutations_ii.kt b/codes/kotlin/chapter_backtracking/permutations_ii.kt similarity index 100% rename from codes/kotlin/chapter_backtracking/permutations_ii/permutations_ii.kt rename to codes/kotlin/chapter_backtracking/permutations_ii.kt diff --git a/codes/kotlin/chapter_backtracking/preorder_traversal_i_compact/preorder_traversal_i_compact.kt b/codes/kotlin/chapter_backtracking/preorder_traversal_i_compact.kt similarity index 100% rename from codes/kotlin/chapter_backtracking/preorder_traversal_i_compact/preorder_traversal_i_compact.kt rename to codes/kotlin/chapter_backtracking/preorder_traversal_i_compact.kt diff --git a/codes/kotlin/chapter_backtracking/preorder_traversal_ii_compact/preorder_traversal_ii_compact.kt b/codes/kotlin/chapter_backtracking/preorder_traversal_ii_compact.kt similarity index 100% rename from codes/kotlin/chapter_backtracking/preorder_traversal_ii_compact/preorder_traversal_ii_compact.kt rename to codes/kotlin/chapter_backtracking/preorder_traversal_ii_compact.kt diff --git a/codes/kotlin/chapter_backtracking/preorder_traversal_iii_compact/preorder_traversal_iii_compact.kt b/codes/kotlin/chapter_backtracking/preorder_traversal_iii_compact.kt similarity index 100% rename from codes/kotlin/chapter_backtracking/preorder_traversal_iii_compact/preorder_traversal_iii_compact.kt rename to codes/kotlin/chapter_backtracking/preorder_traversal_iii_compact.kt diff --git a/codes/kotlin/chapter_backtracking/preorder_traversal_iii_template/preorder_traversal_iii_template.kt b/codes/kotlin/chapter_backtracking/preorder_traversal_iii_template.kt similarity index 100% rename from codes/kotlin/chapter_backtracking/preorder_traversal_iii_template/preorder_traversal_iii_template.kt rename to codes/kotlin/chapter_backtracking/preorder_traversal_iii_template.kt diff --git a/codes/kotlin/chapter_backtracking/subset_sum_i/subset_sum_i.kt b/codes/kotlin/chapter_backtracking/subset_sum_i.kt similarity index 100% rename from codes/kotlin/chapter_backtracking/subset_sum_i/subset_sum_i.kt rename to codes/kotlin/chapter_backtracking/subset_sum_i.kt diff --git a/codes/kotlin/chapter_backtracking/subset_sum_i_naive/subset_sum_i_naive.kt b/codes/kotlin/chapter_backtracking/subset_sum_i_naive.kt similarity index 100% rename from codes/kotlin/chapter_backtracking/subset_sum_i_naive/subset_sum_i_naive.kt rename to codes/kotlin/chapter_backtracking/subset_sum_i_naive.kt diff --git a/codes/kotlin/chapter_backtracking/subset_sum_ii/subset_sum_ii.kt b/codes/kotlin/chapter_backtracking/subset_sum_ii.kt similarity index 100% rename from codes/kotlin/chapter_backtracking/subset_sum_ii/subset_sum_ii.kt rename to codes/kotlin/chapter_backtracking/subset_sum_ii.kt diff --git a/codes/kotlin/chapter_computational_complexity/iteration.kt b/codes/kotlin/chapter_computational_complexity/iteration.kt new file mode 100644 index 000000000..b04a6182d --- /dev/null +++ b/codes/kotlin/chapter_computational_complexity/iteration.kt @@ -0,0 +1,74 @@ +/** + * File: iteration.kt + * Created Time: 2024-01-25 + * Author: curtishd (1023632660@qq.com) + */ + +package chapter_computational_complexity.iteration + +/* for 循环 */ +fun forLoop(n: Int): Int { + var res = 0 + // 循环求和 1, 2, ..., n-1, n + for (i in 1..n) { + res += i + } + return res +} + +/* while 循环 */ +fun whileLoop(n: Int): Int { + var res = 0 + var i = 1 // 初始化条件变量 + // 循环求和 1, 2, ..., n-1, n + while (i <= n) { + res += i + i++ // 更新条件变量 + } + return res +} + +/* while 循环(两次更新) */ +fun whileLoopII(n: Int): Int { + var res = 0 + var i = 1 // 初始化条件变量 + // 循环求和 1, 4, 10, ... + while (i <= n) { + res += i + // 更新条件变量 + i++ + i *= 2 + } + return res +} + +/* 双层 for 循环 */ +fun nestedForLoop(n: Int): String { + val res = StringBuilder() + // 循环 i = 1, 2, ..., n-1, n + for (i in 1..n) { + // 循环 j = 1, 2, ..., n-1, n + for (j in 1..n) { + res.append(" ($i, $j), ") + } + } + return res.toString() +} + +/* Driver Code */ +fun main() { + val n = 5 + var res: Int + + res = forLoop(n) + println("\nfor 循环的求和结果 res = $res") + + res = whileLoop(n) + println("\nwhile 循环的求和结果 res = $res") + + res = whileLoopII(n) + println("\nwhile 循环 (两次更新) 求和结果 res = $res") + + val resStr = nestedForLoop(n) + println("\n双层 for 循环的遍历结果 $resStr") +} \ No newline at end of file diff --git a/codes/kotlin/chapter_computational_complexity/recursion.kt b/codes/kotlin/chapter_computational_complexity/recursion.kt new file mode 100644 index 000000000..9de4253fb --- /dev/null +++ b/codes/kotlin/chapter_computational_complexity/recursion.kt @@ -0,0 +1,76 @@ +/** + * File: recursion.kt + * Created Time: 2024-01-25 + * Author: curtishd (1023632660@qq.com) + */ + +package chapter_computational_complexity.recursion + +import java.util.* + +/* 递归 */ +fun recur(n: Int): Int { + // 终止条件 + if (n == 1) + return 1 + // 递: 递归调用 + val res = recur(n - 1) + // 归: 返回结果 + return n + res +} + +/* 使用迭代模拟递归 */ +fun forLoopRecur(n: Int): Int { + // 使用一个显式的栈来模拟系统调用栈 + val stack = Stack() + var res = 0 + // 递: 递归调用 + for (i in n downTo 0) { + stack.push(i) + } + // 归: 返回结果 + while (stack.isNotEmpty()) { + // 通过“出栈操作”模拟“归” + res += stack.pop() + } + // res = 1+2+3+...+n + return res +} + +/* Kotlin tailrec 关键词使函数实现尾递归优化 */ +tailrec fun tailRecur(n: Int, res: Int): Int { + // 终止条件 + if (n == 0) + return res + // 尾递归调用 + return tailRecur(n - 1, res + n) +} + +/* 斐波那契数列:递归 */ +fun fib(n: Int): Int { + // 终止条件 f(1) = 0, f(2) = 1 + if (n == 1 || n == 2) + return n - 1 + // 递归调用 f(n) = f(n-1) + f(n-2) + val res = fib(n - 1) + fib(n - 2) + // 返回结果 f(n) + return res +} + +/* Driver Code */ +fun main() { + val n = 5 + var res: Int + + res = recur(n) + println("\n递归函数的求和结果 res = $res") + + res = forLoopRecur(n) + println("\n使用迭代模拟递归求和结果 res = $res") + + res = tailRecur(n, 0) + println("\n尾递归函数的求和结果 res = $res") + + res = fib(n) + println("\n斐波那契数列的第 $n 项为 $res") +} \ No newline at end of file diff --git a/codes/kotlin/chapter_computational_complexity/space_complexity.kt b/codes/kotlin/chapter_computational_complexity/space_complexity.kt new file mode 100644 index 000000000..faa9f0ed7 --- /dev/null +++ b/codes/kotlin/chapter_computational_complexity/space_complexity.kt @@ -0,0 +1,109 @@ +/** + * File: space_complexity.kt + * Created Time: 2024-01-25 + * Author: curtishd (1023632660@qq.com) + */ + +package chapter_computational_complexity.space_complexity + +import utils.ListNode +import utils.TreeNode +import utils.printTree + +/* 函数 */ +fun function(): Int { + // 执行某些操作 + return 0 +} + +/* 常数阶 */ +fun constant(n: Int) { + // 常量、变量、对象占用 O(1) 空间 + val a = 0 + var b = 0 + val nums = Array(10000) { 0 } + val node = ListNode(0) + // 循环中的变量占用 O(1) 空间 + for (i in 0..() + for (i in 0..() + for (i in 0..?> = arrayOfNulls(n) + // 二维列表占用 O(n^2) 空间 + val numList: MutableList> = arrayListOf() + for (i in 0..() + for (j in 0.. nums[j + 1]) { + // 交换 nums[j] 与 nums[j + 1] + nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] } + count += 3 // 元素交换包含 3 个单元操作 + } + } + } + return count +} + +/* 指数阶(循环实现) */ +fun exponential(n: Int): Int { + var count = 0 + // 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) + var base = 1 + for (i in 0.. 1) { + n1 /= 2 + count++ + } + return count +} + +/* 对数阶(递归实现) */ +fun logRecur(n: Float): Int { + if (n <= 1) + return 0 + return logRecur(n / 2) + 1 +} + +/* 线性对数阶 */ +fun linearLogRecur(n: Float): Int { + if (n <= 1) + return 1 + var count = linearLogRecur(n / 2) + linearLogRecur(n / 2) + for (i in 0.. { + val nums = IntArray(n) + // 生成数组 nums = { 1, 2, 3, ..., n } + for (i in 0.. int[] + val res = arrayOfNulls(n) + for (i in 0..): Int { + for (i in nums.indices) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) + if (nums[i] == 1) + return i + } + return -1 +} + +/* Driver Code */ +fun main() { + for (i in 0..9) { + val n = 100 + val nums: Array = randomNumbers(n) + val index: Int = findOne(nums) + println("\n数组 [ 1, 2, ..., n ] 被打乱后 = ${nums.contentToString()}") + println("数字 1 的索引为 $index") + } +} \ No newline at end of file