forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Kotlin code for computational complexity (krahets#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.
- Loading branch information
Showing
15 changed files
with
472 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
74 changes: 74 additions & 0 deletions
74
codes/kotlin/chapter_computational_complexity/iteration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* File: iteration.kt | ||
* Created Time: 2024-01-25 | ||
* Author: curtishd ([email protected]) | ||
*/ | ||
|
||
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") | ||
} |
76 changes: 76 additions & 0 deletions
76
codes/kotlin/chapter_computational_complexity/recursion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* File: recursion.kt | ||
* Created Time: 2024-01-25 | ||
* Author: curtishd ([email protected]) | ||
*/ | ||
|
||
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<Int>() | ||
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") | ||
} |
109 changes: 109 additions & 0 deletions
109
codes/kotlin/chapter_computational_complexity/space_complexity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* File: space_complexity.kt | ||
* Created Time: 2024-01-25 | ||
* Author: curtishd ([email protected]) | ||
*/ | ||
|
||
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..<n) { | ||
val c = 0 | ||
} | ||
// 循环中的函数占用 O(1) 空间 | ||
for (i in 0..<n) { | ||
function() | ||
} | ||
} | ||
|
||
/* 线性阶 */ | ||
fun linear(n: Int) { | ||
// 长度为 n 的数组占用 O(n) 空间 | ||
val nums = Array(n) { 0 } | ||
// 长度为 n 的列表占用 O(n) 空间 | ||
val nodes = mutableListOf<ListNode>() | ||
for (i in 0..<n) { | ||
nodes.add(ListNode(i)) | ||
} | ||
// 长度为 n 的哈希表占用 O(n) 空间 | ||
val map = mutableMapOf<Int, String>() | ||
for (i in 0..<n) { | ||
map[i] = i.toString() | ||
} | ||
} | ||
|
||
/* 线性阶(递归实现) */ | ||
fun linearRecur(n: Int) { | ||
println("递归 n = $n") | ||
if (n == 1) | ||
return | ||
linearRecur(n - 1) | ||
} | ||
|
||
/* 平方阶 */ | ||
fun quadratic(n: Int) { | ||
// 矩阵占用 O(n^2) 空间 | ||
val numMatrix: Array<Array<Int>?> = arrayOfNulls(n) | ||
// 二维列表占用 O(n^2) 空间 | ||
val numList: MutableList<MutableList<Int>> = arrayListOf() | ||
for (i in 0..<n) { | ||
val tmp = mutableListOf<Int>() | ||
for (j in 0..<n) { | ||
tmp.add(0) | ||
} | ||
numList.add(tmp) | ||
} | ||
} | ||
|
||
/* 平方阶(递归实现) */ | ||
tailrec fun quadraticRecur(n: Int): Int { | ||
if (n <= 0) | ||
return 0 | ||
// 数组 nums 长度为 n, n-1, ..., 2, 1 | ||
val nums = Array(n) { 0 } | ||
println("递归 n = $n 中的 nums 长度 = ${nums.size}") | ||
return quadraticRecur(n - 1) | ||
} | ||
|
||
/* 指数阶(建立满二叉树) */ | ||
fun buildTree(n: Int): TreeNode? { | ||
if (n == 0) | ||
return null | ||
val root = TreeNode(0) | ||
root.left = buildTree(n - 1) | ||
root.right = buildTree(n - 1) | ||
return root | ||
} | ||
|
||
/* Driver Code */ | ||
fun main() { | ||
val n = 5 | ||
// 常数阶 | ||
constant(n) | ||
// 线性阶 | ||
linear(n) | ||
linearRecur(n) | ||
// 平方阶 | ||
quadratic(n) | ||
quadraticRecur(n) | ||
// 指数阶 | ||
val root: TreeNode? = buildTree(n) | ||
printTree(root) | ||
} |
Oops, something went wrong.