Skip to content

Commit

Permalink
Merge branch 'krahets:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonglja authored Apr 11, 2024
2 parents 815a35c + 49e6a20 commit dae1b39
Show file tree
Hide file tree
Showing 253 changed files with 3,142 additions and 1,874 deletions.
17 changes: 11 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ FROM python:3.10.0-alpine

ENV PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install --upgrade pip
RUN pip install mkdocs-material==9.5.2 mkdocs-glightbox
RUN pip install mkdocs-material==9.5.5 mkdocs-glightbox

WORKDIR /hello-algo

COPY overrides ./build/overrides
COPY docs ./build/docs
COPY docs-en ./build/docs-en
COPY mkdocs.yml ./mkdocs.yml
COPY mkdocs-en.yml ./mkdocs-en.yml

COPY docs ./build/docs
COPY mkdocs.yml mkdocs.yml
RUN mkdocs build -f mkdocs.yml
RUN mkdocs build -f mkdocs-en.yml

COPY zh-hant/docs ./build/zh-hant/docs
COPY zh-hant/mkdocs.yml ./zh-hant/mkdocs.yml
RUN mkdocs build -f ./zh-hant/mkdocs.yml

COPY en/docs ./build/en/docs
COPY en/mkdocs.yml ./en/mkdocs.yml
RUN mkdocs build -f ./en/mkdocs.yml

WORKDIR /hello-algo/site
EXPOSE 8000
Expand Down
3 changes: 2 additions & 1 deletion codes/java/chapter_sorting/radix_sort.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ static void radixSort(int[] nums) {
if (num > m)
m = num;
// 按照从低位到高位的顺序遍历
for (int exp = 1; exp <= m; exp *= 10)
for (int exp = 1; exp <= m; exp *= 10) {
// 对数组元素的第 k 位执行计数排序
// k = 1 -> exp = 1
// k = 2 -> exp = 10
// 即 exp = 10^(k-1)
countingSortDigit(nums, exp);
}
}

public static void main(String[] args) {
Expand Down
4 changes: 2 additions & 2 deletions codes/kotlin/chapter_array_and_linkedlist/linked_list.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fun find(head: ListNode?, target: Int): Int {
var index = 0
var h = head
while (h != null) {
if (h.value == target)
if (h._val == target)
return index
h = h.next
index++
Expand Down Expand Up @@ -79,7 +79,7 @@ fun main() {

/* 访问节点 */
val node: ListNode = access(n0, 3)!!
println("链表中索引 3 处的节点的值 = ${node.value}")
println("链表中索引 3 处的节点的值 = ${node._val}")

/* 查找节点 */
val index: Int = find(n0, 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fun preOrder(root: TreeNode?) {
if (root == null) {
return
}
if (root.value == 7) {
if (root._val == 7) {
// 记录解
res!!.add(root)
}
Expand All @@ -37,7 +37,7 @@ fun main() {
println("\n输出所有值为 7 的节点")
val vals = mutableListOf<Int>()
for (node in res!!) {
vals.add(node.value)
vals.add(node._val)
}
println(vals)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fun preOrder(root: TreeNode?) {
}
// 尝试
path!!.add(root)
if (root.value == 7) {
if (root._val == 7) {
// 记录解
res!!.add(path!!.toMutableList())
}
Expand All @@ -42,10 +42,10 @@ fun main() {

println("\n输出所有根节点到节点 7 的路径")
for (path in res!!) {
val values = mutableListOf<Int>()
val _vals = mutableListOf<Int>()
for (node in path) {
values.add(node.value)
_vals.add(node._val)
}
println(values)
println(_vals)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ var res: MutableList<MutableList<TreeNode>>? = null
/* 前序遍历:例题三 */
fun preOrder(root: TreeNode?) {
// 剪枝
if (root == null || root.value == 3) {
if (root == null || root._val == 3) {
return
}
// 尝试
path!!.add(root)
if (root.value == 7) {
if (root._val == 7) {
// 记录解
res!!.add(path!!.toMutableList())
}
Expand All @@ -43,10 +43,10 @@ fun main() {

println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点")
for (path in res!!) {
val values = mutableListOf<Int>()
val _vals = mutableListOf<Int>()
for (node in path) {
values.add(node.value)
_vals.add(node._val)
}
println(values)
println(_vals)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import utils.printTree

/* 判断当前状态是否为解 */
fun isSolution(state: MutableList<TreeNode?>): Boolean {
return state.isNotEmpty() && state[state.size - 1]?.value == 7
return state.isNotEmpty() && state[state.size - 1]?._val == 7
}

/* 记录解 */
Expand All @@ -21,7 +21,7 @@ fun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<MutableList<

/* 判断在当前状态下,该选择是否合法 */
fun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {
return choice != null && choice.value != 3
return choice != null && choice._val != 3
}

/* 更新状态 */
Expand Down Expand Up @@ -74,7 +74,7 @@ fun main() {
val vals = mutableListOf<Int>()
for (node in path!!) {
if (node != null) {
vals.add(node.value)
vals.add(node._val)
}
}
println(vals)
Expand Down
2 changes: 1 addition & 1 deletion codes/kotlin/chapter_backtracking/subset_sum_i.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ fun main() {

println("输入数组 nums = ${nums.contentToString()}, target = $target")
println("所有和等于 $target 的子集 res = $res")
}
}
2 changes: 1 addition & 1 deletion codes/kotlin/chapter_backtracking/subset_sum_ii.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ fun main() {

println("输入数组 nums = ${nums.contentToString()}, target = $target")
println("所有和等于 $target 的子集 res = $res")
}
}
2 changes: 1 addition & 1 deletion codes/kotlin/chapter_computational_complexity/recursion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ fun main() {

res = fib(n)
println("\n斐波那契数列的第 $n 项为 $res")
}
}
2 changes: 1 addition & 1 deletion codes/kotlin/chapter_dynamic_programming/coin_change.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ fun main() {
// 空间优化后的动态规划
res = coinChangeDPComp(coins, amt)
println("凑到目标金额所需的最少硬币数量为 $res")
}
}
36 changes: 18 additions & 18 deletions codes/kotlin/chapter_dynamic_programming/knapsack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlin.math.max
/* 0-1 背包:暴力搜索 */
fun knapsackDFS(
wgt: IntArray,
value: IntArray,
_val: IntArray,
i: Int,
c: Int
): Int {
Expand All @@ -21,19 +21,19 @@ fun knapsackDFS(
}
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFS(wgt, value, i - 1, c)
return knapsackDFS(wgt, _val, i - 1, c)
}
// 计算不放入和放入物品 i 的最大价值
val no = knapsackDFS(wgt, value, i - 1, c)
val yes = knapsackDFS(wgt, value, i - 1, c - wgt[i - 1]) + value[i - 1]
val no = knapsackDFS(wgt, _val, i - 1, c)
val yes = knapsackDFS(wgt, _val, i - 1, c - wgt[i - 1]) + _val[i - 1]
// 返回两种方案中价值更大的那一个
return max(no, yes)
}

/* 0-1 背包:记忆化搜索 */
fun knapsackDFSMem(
wgt: IntArray,
value: IntArray,
_val: IntArray,
mem: Array<IntArray>,
i: Int,
c: Int
Expand All @@ -48,11 +48,11 @@ fun knapsackDFSMem(
}
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFSMem(wgt, value, mem, i - 1, c)
return knapsackDFSMem(wgt, _val, mem, i - 1, c)
}
// 计算不放入和放入物品 i 的最大价值
val no = knapsackDFSMem(wgt, value, mem, i - 1, c)
val yes = knapsackDFSMem(wgt, value, mem, i - 1, c - wgt[i - 1]) + value[i - 1]
val no = knapsackDFSMem(wgt, _val, mem, i - 1, c)
val yes = knapsackDFSMem(wgt, _val, mem, i - 1, c - wgt[i - 1]) + _val[i - 1]
// 记录并返回两种方案中价值更大的那一个
mem[i][c] = max(no, yes)
return mem[i][c]
Expand All @@ -61,7 +61,7 @@ fun knapsackDFSMem(
/* 0-1 背包:动态规划 */
fun knapsackDP(
wgt: IntArray,
value: IntArray,
_val: IntArray,
cap: Int
): Int {
val n = wgt.size
Expand All @@ -75,7 +75,7 @@ fun knapsackDP(
dp[i][c] = dp[i - 1][c]
} else {
// 不选和选物品 i 这两种方案的较大值
dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + value[i - 1])
dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + _val[i - 1])
}
}
}
Expand All @@ -85,7 +85,7 @@ fun knapsackDP(
/* 0-1 背包:空间优化后的动态规划 */
fun knapsackDPComp(
wgt: IntArray,
value: IntArray,
_val: IntArray,
cap: Int
): Int {
val n = wgt.size
Expand All @@ -98,7 +98,7 @@ fun knapsackDPComp(
if (wgt[i - 1] <= c) {
// 不选和选物品 i 这两种方案的较大值
dp[c] =
max(dp[c], dp[c - wgt[i - 1]] + value[i - 1])
max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
}
}
}
Expand All @@ -108,27 +108,27 @@ fun knapsackDPComp(
/* Driver Code */
fun main() {
val wgt = intArrayOf(10, 20, 30, 40, 50)
val value = intArrayOf(50, 120, 150, 210, 240)
val _val = intArrayOf(50, 120, 150, 210, 240)
val cap = 50
val n = wgt.size

// 暴力搜索
var res = knapsackDFS(wgt, value, n, cap)
var res = knapsackDFS(wgt, _val, n, cap)
println("不超过背包容量的最大物品价值为 $res")

// 记忆化搜索
val mem = Array(n + 1) { IntArray(cap + 1) }
for (row in mem) {
row.fill(-1)
}
res = knapsackDFSMem(wgt, value, mem, n, cap)
res = knapsackDFSMem(wgt, _val, mem, n, cap)
println("不超过背包容量的最大物品价值为 $res")

// 动态规划
res = knapsackDP(wgt, value, cap)
res = knapsackDP(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")

// 空间优化后的动态规划
res = knapsackDPComp(wgt, value, cap)
res = knapsackDPComp(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")
}
}
14 changes: 7 additions & 7 deletions codes/kotlin/chapter_dynamic_programming/unbounded_knapsack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package chapter_dynamic_programming
import kotlin.math.max

/* 完全背包:动态规划 */
fun unboundedKnapsackDP(wgt: IntArray, value: IntArray, cap: Int): Int {
fun unboundedKnapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {
val n = wgt.size
// 初始化 dp 表
val dp = Array(n + 1) { IntArray(cap + 1) }
Expand All @@ -21,7 +21,7 @@ fun unboundedKnapsackDP(wgt: IntArray, value: IntArray, cap: Int): Int {
dp[i][c] = dp[i - 1][c]
} else {
// 不选和选物品 i 这两种方案的较大值
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + value[i - 1])
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + _val[i - 1])
}
}
}
Expand All @@ -31,7 +31,7 @@ fun unboundedKnapsackDP(wgt: IntArray, value: IntArray, cap: Int): Int {
/* 完全背包:空间优化后的动态规划 */
fun unboundedKnapsackDPComp(
wgt: IntArray,
value: IntArray,
_val: IntArray,
cap: Int
): Int {
val n = wgt.size
Expand All @@ -45,7 +45,7 @@ fun unboundedKnapsackDPComp(
dp[c] = dp[c]
} else {
// 不选和选物品 i 这两种方案的较大值
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + value[i - 1])
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
}
}
}
Expand All @@ -55,14 +55,14 @@ fun unboundedKnapsackDPComp(
/* Driver Code */
fun main() {
val wgt = intArrayOf(1, 2, 3)
val value = intArrayOf(5, 11, 15)
val _val = intArrayOf(5, 11, 15)
val cap = 4

// 动态规划
var res = unboundedKnapsackDP(wgt, value, cap)
var res = unboundedKnapsackDP(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")

// 空间优化后的动态规划
res = unboundedKnapsackDPComp(wgt, value, cap)
res = unboundedKnapsackDPComp(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")
}
4 changes: 2 additions & 2 deletions codes/kotlin/chapter_graph/graph_adjacency_list.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
for (pair in adjList.entries) {
val tmp = mutableListOf<Int>()
for (vertex in pair.value) {
tmp.add(vertex.value)
tmp.add(vertex._val)
}
println("${pair.key.value}: $tmp,")
println("${pair.key._val}: $tmp,")
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions codes/kotlin/chapter_graph/graph_adjacency_matrix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
}

/* 添加顶点 */
fun addVertex(value: Int) {
fun addVertex(_val: Int) {
val n = size()
// 向顶点列表中添加新顶点的值
vertices.add(value)
vertices.add(_val)
// 在邻接矩阵中添加一行
val newRow = mutableListOf<Int>()
for (j in 0..<n) {
Expand Down
Loading

0 comments on commit dae1b39

Please sign in to comment.