From c23e576da4519d4d327fa9422917617e6e552c7e Mon Sep 17 00:00:00 2001 From: krahets Date: Sun, 31 Mar 2024 03:53:04 +0800 Subject: [PATCH] build --- docs/chapter_appendix/terminology.md | 4 +- docs/chapter_array_and_linkedlist/array.md | 102 +++++++++ .../linked_list.md | 127 +++++++++++ docs/chapter_array_and_linkedlist/list.md | 198 ++++++++++++++++++ .../backtracking_algorithm.md | 60 ++++++ docs/chapter_backtracking/n_queens_problem.md | 8 + .../permutations_problem.md | 16 ++ .../subset_sum_problem.md | 24 +++ .../iteration_and_recursion.md | 51 ++++- .../space_complexity.md | 95 ++++++++- .../time_complexity.md | 143 +++++++++++++ .../basic_data_types.md | 10 + .../binary_search_recur.md | 8 + .../build_binary_tree_problem.md | 8 + .../hanota_problem.md | 10 + .../dp_problem_features.md | 18 ++ .../dp_solution_pipeline.md | 24 +++ .../edit_distance_problem.md | 12 ++ .../intro_to_dynamic_programming.md | 36 ++++ .../knapsack_problem.md | 24 +++ .../unbounded_knapsack_problem.md | 36 ++++ docs/chapter_graph/graph_operations.md | 12 ++ docs/chapter_graph/graph_traversal.md | 14 ++ .../fractional_knapsack_problem.md | 8 + docs/chapter_greedy/greedy_algorithm.md | 6 + docs/chapter_greedy/max_capacity_problem.md | 6 + .../max_product_cutting_problem.md | 6 + docs/chapter_hashing/hash_algorithm.md | 40 ++++ docs/chapter_hashing/hash_collision.md | 12 ++ docs/chapter_hashing/hash_map.md | 49 +++++ docs/chapter_heap/build_heap.md | 6 + docs/chapter_heap/heap.md | 69 ++++++ docs/chapter_heap/top_k.md | 6 + docs/chapter_preface/suggestions.md | 6 + docs/chapter_searching/binary_search.md | 12 ++ docs/chapter_searching/binary_search_edge.md | 12 ++ .../binary_search_insertion.md | 12 ++ .../replace_linear_by_hashing.md | 12 ++ docs/chapter_sorting/bubble_sort.md | 12 ++ docs/chapter_sorting/bucket_sort.md | 16 +- docs/chapter_sorting/counting_sort.md | 12 ++ docs/chapter_sorting/heap_sort.md | 8 + docs/chapter_sorting/insertion_sort.md | 6 + docs/chapter_sorting/merge_sort.md | 8 + docs/chapter_sorting/quick_sort.md | 26 +++ docs/chapter_sorting/radix_sort.md | 10 + docs/chapter_sorting/selection_sort.md | 6 + docs/chapter_stack_and_queue/deque.md | 46 +++- docs/chapter_stack_and_queue/queue.md | 38 ++++ docs/chapter_stack_and_queue/stack.md | 43 +++- .../array_representation_of_tree.md | 14 ++ docs/chapter_tree/avl_tree.md | 59 ++++++ docs/chapter_tree/binary_search_tree.md | 18 ++ docs/chapter_tree/binary_tree.md | 37 ++++ docs/chapter_tree/binary_tree_traversal.md | 16 ++ en/docs/chapter_array_and_linkedlist/array.md | 92 ++++++++ .../linked_list.md | 56 +++++ en/docs/chapter_array_and_linkedlist/list.md | 94 +++++++++ .../iteration_and_recursion.md | 51 ++++- .../space_complexity.md | 38 ++++ .../time_complexity.md | 74 +++++++ en/docs/chapter_hashing/hash_algorithm.md | 12 ++ en/docs/chapter_hashing/hash_collision.md | 12 ++ en/docs/chapter_hashing/hash_map.md | 8 + en/docs/chapter_stack_and_queue/deque.md | 16 +- en/docs/chapter_stack_and_queue/queue.md | 12 ++ en/docs/chapter_stack_and_queue/stack.md | 17 +- overrides/stylesheets/extra.css | 2 +- 68 files changed, 2139 insertions(+), 22 deletions(-) diff --git a/docs/chapter_appendix/terminology.md b/docs/chapter_appendix/terminology.md index e684a026c..96b982e32 100644 --- a/docs/chapter_appendix/terminology.md +++ b/docs/chapter_appendix/terminology.md @@ -112,7 +112,7 @@ comments: true | adjacency list | 邻接表 | 鄰接表 | | breadth-first search | 广度优先搜索 | 廣度優先搜尋 | | depth-first search | 深度优先搜索 | 深度優先搜尋 | -| binary search | 二分查找 | 二分查找 | +| binary search | 二分查找 | 二分搜尋 | | searching algorithm | 搜索算法 | 搜尋演算法 | | sorting algorithm | 排序算法 | 排序演算法 | | selection sort | 选择排序 | 選擇排序 | @@ -136,7 +136,7 @@ comments: true | $n$-queens problem | $n$ 皇后问题 | $n$ 皇后問題 | | dynamic programming | 动态规划 | 動態規劃 | | initial state | 初始状态 | 初始狀態 | -| state-transition equation | 状态转移方程 | 狀態轉移方程 | +| state-transition equation | 状态转移方程 | 狀態轉移方程 | | knapsack problem | 背包问题 | 背包問題 | | edit distance problem | 编辑距离问题 | 編輯距離問題 | | greedy algorithm | 贪心算法 | 貪婪演算法 | diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index 2c110d915..051145069 100755 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -114,7 +114,17 @@ comments: true === "Kotlin" ```kotlin title="array.kt" + /* 初始化数组 */ + var arr = IntArray(5) // { 0, 0, 0, 0, 0 } + var nums = intArrayOf(1, 3, 2, 5, 4) + ``` +=== "Ruby" + + ```ruby title="array.rb" + # 初始化数组 + arr = Array.new(5, 0) + nums = [1, 3, 2, 5, 4] ``` === "Zig" @@ -298,6 +308,19 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="array.rb" + ### 随机访问元素 ### + def random_access(nums) + # 在区间 [0, nums.length) 中随机抽取一个数字 + random_index = Random.rand 0...(nums.length - 1) + + # 获取并返回随机元素 + nums[random_index] + end + ``` + === "Zig" ```zig title="array.zig" @@ -492,6 +515,21 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="array.rb" + ### 在数组的索引 index 处插入元素 num ### + def insert(nums, num, index) + # 把索引 index 以及之后的所有元素向后移动一位 + for i in (nums.length - 1).downto(index + 1) + nums[i] = nums[i - 1] + end + + # 将 num 赋给 index 处的元素 + nums[index] = num + end + ``` + === "Zig" ```zig title="array.zig" @@ -665,6 +703,18 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="array.rb" + ### 删除索引 index 处的元素 ### + def remove(nums, index) + # 把索引 index 之后的所有元素向前移动一位 + for i in index...nums.length + nums[i] = nums[i + 1] || 0 + end + end + ``` + === "Zig" ```zig title="array.zig" @@ -905,6 +955,25 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="array.rb" + ### 遍历数组 ### + def traverse(nums) + count = 0 + + # 通过索引遍历数组 + for i in 0...nums.length + count += nums[i] + end + + # 直接遍历数组元素 + for num in nums + count += num + end + end + ``` + === "Zig" ```zig title="array.zig" @@ -1092,6 +1161,19 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="array.rb" + ### 在数组中查找指定元素 ### + def find(nums, target) + for i in 0...nums.length + return i if nums[i] == target + end + + -1 + end + ``` + === "Zig" ```zig title="array.zig" @@ -1315,6 +1397,26 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="array.rb" + ### 扩展数组长度 ### + # 请注意,Ruby 的 Array 是动态数组,可以直接扩展 + # 为了方便学习,本函数将 Array 看作长度不可变的数组 + def extend(nums, enlarge) + # 初始化一个扩展长度后的数组 + res = Array.new(nums.length + enlarge, 0) + + # 将原数组中的所有元素复制到新数组 + for i in 0...nums.length + res[i] = nums[i] + end + + # 返回扩展后的新数组 + res + end + ``` + === "Zig" ```zig title="array.zig" diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 25fcc7d02..6145922d5 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -168,7 +168,27 @@ comments: true === "Kotlin" ```kotlin title="" + /* 链表节点类 */ + // 构造方法 + class ListNode(x: Int) { + val _val: Int = x // 节点值 + val next: ListNode? = null // 指向下一个节点的引用 + } + ``` +=== "Ruby" + + ```ruby title="" + # 链表节点类 + class ListNode + attr_accessor :val # 节点值 + attr_accessor :next # 指向下一节点的引用 + + def initialize(val=nil, next_node=nil) + @val = val || 0 + @next = next_node + end + end ``` === "Zig" @@ -388,7 +408,35 @@ comments: true === "Kotlin" ```kotlin title="linked_list.kt" + /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ + // 初始化各个节点 + val n0 = ListNode(1) + val n1 = ListNode(3) + val n2 = ListNode(2) + val n3 = ListNode(5) + val n4 = ListNode(4) + // 构建节点之间的引用 + n0.next = n1; + n1.next = n2; + n2.next = n3; + n3.next = n4; + ``` +=== "Ruby" + + ```ruby title="linked_list.rb" + # 初始化链表 1 -> 3 -> 2 -> 5 -> 4 + # 初始化各个节点 + n0 = ListNode.new 1 + n1 = ListNode.new 3 + n2 = ListNode.new 2 + n3 = ListNode.new 5 + n4 = ListNode.new 4 + # 构建节点之间的引用 + n0.next = n1 + n1.next = n2 + n2.next = n3 + n3.next = n4 ``` === "Zig" @@ -557,6 +605,18 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="linked_list.rb" + ### 在链表的节点 n0 之后插入节点 _p ### + # Ruby 的 `p` 是一个内置函数, `P` 是一个常量,所以可以使用 `_p` 代替 + def insert(n0, _p) + n1 = n0.next + _p.next = n1 + n0.next = _p + end + ``` + === "Zig" ```zig title="linked_list.zig" @@ -757,6 +817,20 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="linked_list.rb" + ### 删除链表的节点 n0 之后的首个节点 ### + def remove(n0) + return if n0.next.nil? + + # n0 -> remove_node -> n1 + remove_node = n0.next + n1 = remove_node.next + n0.next = n1 + end + ``` + === "Zig" ```zig title="linked_list.zig" @@ -950,6 +1024,20 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="linked_list.rb" + ### 访问链表中索引为 index 的节点 ### + def access(head, index) + for i in 0...index + return nil if head.nil? + head = head.next + end + + head + end + ``` + === "Zig" ```zig title="linked_list.zig" @@ -1169,6 +1257,22 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="linked_list.rb" + ### 在链表中查找值为 target 的首个节点 ### + def find(head, target) + index = 0 + while head + return index if head.val == target + head = head.next + index += 1 + end + + -1 + end + ``` + === "Zig" ```zig title="linked_list.zig" @@ -1389,7 +1493,30 @@ comments: true === "Kotlin" ```kotlin title="" + /* 双向链表节点类 */ + // 构造方法 + class ListNode(x: Int) { + val _val: Int = x // 节点值 + val next: ListNode? = null // 指向后继节点的引用 + val prev: ListNode? = null // 指向前驱节点的引用 + } + ``` +=== "Ruby" + + ```ruby title="" + # 双向链表节点类 + class ListNode + attr_accessor :val # 节点值 + attr_accessor :next # 指向后继节点的引用 + attr_accessor :prev # 指向前驱节点的引用 + + def initialize(val=nil, next_node=nil, prev_node=nil) + @val = val || 0 + @next = next_node + @prev = prev_node + end + end ``` === "Zig" diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index 46f1e2753..7232800f4 100755 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -133,7 +133,22 @@ comments: true === "Kotlin" ```kotlin title="list.kt" + /* 初始化列表 */ + // 无初始值 + var nums1 = listOf() + // 有初始值 + var numbers = arrayOf(1, 3, 2, 5, 4) + var nums = numbers.toMutableList() + ``` +=== "Ruby" + + ```ruby title="list.rb" + # 初始化列表 + # 无初始值 + nums1 = [] + # 有初始值 + nums = [1, 3, 2, 5, 4] ``` === "Zig" @@ -262,7 +277,19 @@ comments: true === "Kotlin" ```kotlin title="list.kt" + /* 访问元素 */ + val num = nums[1] // 访问索引 1 处的元素 + /* 更新元素 */ + nums[1] = 0 // 将索引 1 处的元素更新为 0 + ``` +=== "Ruby" + + ```ruby title="list.rb" + # 访问元素 + num = nums[1] + # 更新元素 + nums[1] = 0 ``` === "Zig" @@ -493,7 +520,41 @@ comments: true === "Kotlin" ```kotlin title="list.kt" + /* 清空列表 */ + nums.clear(); + /* 在尾部添加元素 */ + nums.add(1); + nums.add(3); + nums.add(2); + nums.add(5); + nums.add(4); + + /* 在中间插入元素 */ + nums.add(3, 6); // 在索引 3 处插入数字 6 + + /* 删除元素 */ + nums.remove(3); // 删除索引 3 处的元素 + ``` + +=== "Ruby" + + ```ruby title="list.rb" + # 清空列表 + nums.clear + + # 在尾部添加元素 + nums << 1 + nums << 3 + nums << 2 + nums << 5 + nums << 4 + + # 在中间插入元素 + nums.insert 3, 6 + + # 删除元素 + nums.delete_at 3 ``` === "Zig" @@ -690,7 +751,32 @@ comments: true === "Kotlin" ```kotlin title="list.kt" + /* 通过索引遍历列表 */ + var count = 0 + for (i in nums.indices) { + count += nums[i] + } + /* 直接遍历列表元素 */ + for (num in nums) { + count += num + } + ``` + +=== "Ruby" + + ```ruby title="list.rb" + # 通过索引遍历列表 + count = 0 + for i in 0...nums.length + count += nums[i] + end + + # 直接遍历列表元素 + count = 0 + for num in nums + count += num + end ``` === "Zig" @@ -809,7 +895,17 @@ comments: true === "Kotlin" ```kotlin title="list.kt" + /* 拼接两个列表 */ + val nums1 = intArrayOf(6, 8, 7, 10, 9).toMutableList() + nums.addAll(nums1) // 将列表 nums1 拼接到 nums 之后 + ``` +=== "Ruby" + + ```ruby title="list.rb" + # 拼接两个列表 + nums1 = [6, 8, 7, 10, 9] + nums += nums1 ``` === "Zig" @@ -910,7 +1006,15 @@ comments: true === "Kotlin" ```kotlin title="list.kt" + /* 排序列表 */ + nums.sort() // 排序后,列表元素从小到大排列 + ``` +=== "Ruby" + + ```ruby title="list.rb" + # 排序列表 + nums = nums.sort { |a, b| a <=> b } ``` === "Zig" @@ -2171,6 +2275,100 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="my_list.rb" + ### 列表类 ### + class MyList + attr_reader :size # 获取列表长度(当前元素数量) + attr_reader :capacity # 获取列表容量 + + ### 构造方法 ### + def initialize + @capacity = 10 + @size = 0 + @extend_ratio = 2 + @arr = Array.new capacity + end + + ### 访问元素 ### + def get(index) + # 索引如果越界,则抛出异常,下同 + raise IndexError, "索引越界" if index < 0 || index >= size + @arr[index] + end + + ### 访问元素 ### + def set(index, num) + raise IndexError, "索引越界" if index < 0 || index >= size + @arr[index] = num + end + + ### 在尾部添加元素 ### + def add(num) + # 元素数量超出容量时,触发扩容机制 + extend_capacity if size == capacity + @arr[size] = num + + # 更新元素数量 + @size += 1 + end + + ### 在中间插入元素 ### + def insert(index, num) + raise IndexError, "索引越界" if index < 0 || index >= size + + # 元素数量超出容量时,触发扩容机制 + extend_capacity if size == capacity + + # 将索引 index 以及之后的元素都向后移动一位 + for j in (size - 1).downto(index) + @arr[j + 1] = @arr[j] + end + @arr[index] = num + + # 更新元素数量 + @size += 1 + end + + ### 删除元素 ### + def remove(index) + raise IndexError, "索引越界" if index < 0 || index >= size + num = @arr[index] + + # 将将索引 index 之后的元素都向前移动一位 + for j in index...size + @arr[j] = @arr[j + 1] + end + + # 更新元素数量 + @size -= 1 + + # 返回被删除的元素 + num + end + + ### 列表扩容 ### + def extend_capacity + # 新建一个长度为原数组 extend_ratio 倍的新数组,并将原数组复制到新数组 + arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1)) + # 更新列表容量 + @capacity = arr.length + end + + ### 将列表转换为数组 ### + def to_array + sz = size + # 仅转换有效长度范围内的列表元素 + arr = Array.new sz + for i in 0...sz + arr[i] = get i + end + arr + end + end + ``` + === "Zig" ```zig title="my_list.zig" diff --git a/docs/chapter_backtracking/backtracking_algorithm.md b/docs/chapter_backtracking/backtracking_algorithm.md index 6654687a5..fad8431ca 100644 --- a/docs/chapter_backtracking/backtracking_algorithm.md +++ b/docs/chapter_backtracking/backtracking_algorithm.md @@ -217,6 +217,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="preorder_traversal_i_compact.rb" + [class]{}-[func]{pre_order} + ``` + === "Zig" ```zig title="preorder_traversal_i_compact.zig" @@ -513,6 +519,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="preorder_traversal_ii_compact.rb" + [class]{}-[func]{pre_order} + ``` + === "Zig" ```zig title="preorder_traversal_ii_compact.zig" @@ -851,6 +863,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="preorder_traversal_iii_compact.rb" + [class]{}-[func]{pre_order} + ``` + === "Zig" ```zig title="preorder_traversal_iii_compact.zig" @@ -1159,6 +1177,32 @@ comments: true === "Kotlin" ```kotlin title="" + /* 回溯算法框架 */ + fun backtrack(state: State?, choices: List, res: List?) { + // 判断是否为解 + if (isSolution(state)) { + // 记录解 + recordSolution(state, res) + // 不再继续搜索 + return + } + // 遍历所有选择 + for (choice in choices) { + // 剪枝:判断选择是否合法 + if (isValid(state, choice)) { + // 尝试:做出选择,更新状态 + makeChoice(state, choice) + backtrack(state, choices, res) + // 回退:撤销选择,恢复到之前的状态 + undoChoice(state, choice) + } + } + } + ``` + +=== "Ruby" + + ```ruby title="" ``` @@ -1796,6 +1840,22 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="preorder_traversal_iii_template.rb" + [class]{}-[func]{is_solution} + + [class]{}-[func]{record_solution} + + [class]{}-[func]{is_valid} + + [class]{}-[func]{make_choice} + + [class]{}-[func]{undo_choice} + + [class]{}-[func]{backtrack} + ``` + === "Zig" ```zig title="preorder_traversal_iii_template.zig" diff --git a/docs/chapter_backtracking/n_queens_problem.md b/docs/chapter_backtracking/n_queens_problem.md index 646771d24..6539e03ff 100644 --- a/docs/chapter_backtracking/n_queens_problem.md +++ b/docs/chapter_backtracking/n_queens_problem.md @@ -706,6 +706,14 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="n_queens.rb" + [class]{}-[func]{backtrack} + + [class]{}-[func]{n_queens} + ``` + === "Zig" ```zig title="n_queens.zig" diff --git a/docs/chapter_backtracking/permutations_problem.md b/docs/chapter_backtracking/permutations_problem.md index e29edd4a6..8653a1482 100644 --- a/docs/chapter_backtracking/permutations_problem.md +++ b/docs/chapter_backtracking/permutations_problem.md @@ -503,6 +503,14 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="permutations_i.rb" + [class]{}-[func]{backtrack} + + [class]{}-[func]{permutations_i} + ``` + === "Zig" ```zig title="permutations_i.zig" @@ -1021,6 +1029,14 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="permutations_ii.rb" + [class]{}-[func]{backtrack} + + [class]{}-[func]{permutations_ii} + ``` + === "Zig" ```zig title="permutations_ii.zig" diff --git a/docs/chapter_backtracking/subset_sum_problem.md b/docs/chapter_backtracking/subset_sum_problem.md index 386718046..749d64ad1 100644 --- a/docs/chapter_backtracking/subset_sum_problem.md +++ b/docs/chapter_backtracking/subset_sum_problem.md @@ -467,6 +467,14 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="subset_sum_i_naive.rb" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subset_sum_i_naive} + ``` + === "Zig" ```zig title="subset_sum_i_naive.zig" @@ -1000,6 +1008,14 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="subset_sum_i.rb" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subset_sum_i} + ``` + === "Zig" ```zig title="subset_sum_i.zig" @@ -1579,6 +1595,14 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="subset_sum_ii.rb" + [class]{}-[func]{backtrack} + + [class]{}-[func]{subset_sum_ii} + ``` + === "Zig" ```zig title="subset_sum_ii.zig" diff --git a/docs/chapter_computational_complexity/iteration_and_recursion.md b/docs/chapter_computational_complexity/iteration_and_recursion.md index 8cc8be30a..b60974327 100644 --- a/docs/chapter_computational_complexity/iteration_and_recursion.md +++ b/docs/chapter_computational_complexity/iteration_and_recursion.md @@ -182,6 +182,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="iteration.rb" + [class]{}-[func]{for_loop} + ``` + === "Zig" ```zig title="iteration.zig" @@ -408,6 +414,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="iteration.rb" + [class]{}-[func]{while_loop} + ``` + === "Zig" ```zig title="iteration.zig" @@ -649,6 +661,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="iteration.rb" + [class]{}-[func]{while_loop_ii} + ``` + === "Zig" ```zig title="iteration.zig" @@ -883,6 +901,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="iteration.rb" + [class]{}-[func]{nested_for_loop} + ``` + === "Zig" ```zig title="iteration.zig" @@ -1112,6 +1136,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="recursion.rb" + [class]{}-[func]{recur} + ``` + === "Zig" ```zig title="recursion.zig" @@ -1318,8 +1348,9 @@ comments: true === "Kotlin" ```kotlin title="recursion.kt" - /* Kotlin tailrec 关键词使函数实现尾递归优化 */ + /* 尾递归 */ tailrec fun tailRecur(n: Int, res: Int): Int { + // 添加 tailrec 关键词,以开启尾递归优化 // 终止条件 if (n == 0) return res @@ -1328,6 +1359,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="recursion.rb" + [class]{}-[func]{tail_recur} + ``` + === "Zig" ```zig title="recursion.zig" @@ -1554,6 +1591,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="recursion.rb" + [class]{}-[func]{fib} + ``` + === "Zig" ```zig title="recursion.zig" @@ -1890,6 +1933,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="recursion.rb" + [class]{}-[func]{for_loop_recur} + ``` + === "Zig" ```zig title="recursion.zig" diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index ba694ceb8..c91274919 100755 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -318,6 +318,29 @@ comments: true === "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 // 输出数据 + } + ``` + +=== "Ruby" + + ```ruby title="" ``` @@ -470,6 +493,18 @@ comments: true === "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) + } + } + ``` + +=== "Ruby" + + ```ruby title="" ``` @@ -683,7 +718,7 @@ comments: true } } /* 递归 O(n) */ - void recur(n: i32) { + fn recur(n: i32) { if n == 1 { return; } @@ -714,6 +749,26 @@ comments: true === "Kotlin" ```kotlin title="" + fun function(): Int { + // 执行某些操作 + return 0 + } + /* 循环 O(1) */ + fun loop(n: Int) { + for (i in 0..(12836, "小哈") + val hashTup = arr.hashCode() + // 数组 [12836, 小哈] 的哈希值为 189568618 + + val obj = ListNode(0) + val hashObj = obj.hashCode() + // 节点对象 utils.ListNode@1d81eb93 的哈希值为 495053715 + ``` + +=== "Ruby" + + ```ruby title="built_in_hash.rb" ``` diff --git a/docs/chapter_hashing/hash_collision.md b/docs/chapter_hashing/hash_collision.md index fa50ee408..c32f33364 100644 --- a/docs/chapter_hashing/hash_collision.md +++ b/docs/chapter_hashing/hash_collision.md @@ -1426,6 +1426,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="hash_map_chaining.rb" + [class]{HashMapChaining}-[func]{} + ``` + === "Zig" ```zig title="hash_map_chaining.zig" @@ -3072,6 +3078,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="hash_map_open_addressing.rb" + [class]{HashMapOpenAddressing}-[func]{} + ``` + === "Zig" ```zig title="hash_map_open_addressing.zig" diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 0183aed51..36988ae09 100755 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -280,6 +280,29 @@ comments: true === "Kotlin" ```kotlin title="hash_map.kt" + /* 初始化哈希表 */ + val map = HashMap() + + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + map[12836] = "小哈" + map[15937] = "小啰" + map[16750] = "小算" + map[13276] = "小法" + map[10583] = "小鸭" + + /* 查询操作 */ + // 向哈希表中输入键 key ,得到值 value + val name = map[15937] + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + map.remove(10583) + ``` + +=== "Ruby" + + ```ruby title="hash_map.rb" ``` @@ -482,6 +505,24 @@ comments: true === "Kotlin" ```kotlin title="hash_map.kt" + /* 遍历哈希表 */ + // 遍历键值对 key->value + for ((key, value) in map) { + println("$key -> $value") + } + // 单独遍历键 key + for (key in map.keys) { + println(key) + } + // 单独遍历值 value + for (_val in map.values) { + println(_val) + } + ``` + +=== "Ruby" + + ```ruby title="hash_map.rb" ``` @@ -1697,6 +1738,14 @@ index = hash(key) % capacity } ``` +=== "Ruby" + + ```ruby title="array_hash_map.rb" + [class]{Pair}-[func]{} + + [class]{ArrayHashMap}-[func]{} + ``` + === "Zig" ```zig title="array_hash_map.zig" diff --git a/docs/chapter_heap/build_heap.md b/docs/chapter_heap/build_heap.md index 728780dab..c19bdd6af 100644 --- a/docs/chapter_heap/build_heap.md +++ b/docs/chapter_heap/build_heap.md @@ -305,6 +305,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="my_heap.rb" + [class]{MaxHeap}-[func]{__init__} + ``` + === "Zig" ```zig title="my_heap.zig" diff --git a/docs/chapter_heap/heap.md b/docs/chapter_heap/heap.md index 3d0277c55..1e84af714 100644 --- a/docs/chapter_heap/heap.md +++ b/docs/chapter_heap/heap.md @@ -378,6 +378,43 @@ comments: true === "Kotlin" ```kotlin title="heap.kt" + /* 初始化堆 */ + // 初始化小顶堆 + var minHeap = PriorityQueue() + // 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可) + val maxHeap = PriorityQueue { a: Int, b: Int -> b - a } + + /* 元素入堆 */ + maxHeap.offer(1) + maxHeap.offer(3) + maxHeap.offer(2) + maxHeap.offer(5) + maxHeap.offer(4) + + /* 获取堆顶元素 */ + var peek = maxHeap.peek() // 5 + + /* 堆顶元素出堆 */ + // 出堆元素会形成一个从大到小的序列 + peek = maxHeap.poll() // 5 + peek = maxHeap.poll() // 4 + peek = maxHeap.poll() // 3 + peek = maxHeap.poll() // 2 + peek = maxHeap.poll() // 1 + + /* 获取堆大小 */ + val size = maxHeap.size + + /* 判断堆是否为空 */ + val isEmpty = maxHeap.isEmpty() + + /* 输入列表并建堆 */ + minHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4)) + ``` + +=== "Ruby" + + ```ruby title="heap.rb" ``` @@ -636,6 +673,16 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="my_heap.rb" + [class]{MaxHeap}-[func]{left} + + [class]{MaxHeap}-[func]{right} + + [class]{MaxHeap}-[func]{parent} + ``` + === "Zig" ```zig title="my_heap.zig" @@ -767,6 +814,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="my_heap.rb" + [class]{MaxHeap}-[func]{peek} + ``` + === "Zig" ```zig title="my_heap.zig" @@ -1155,6 +1208,14 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="my_heap.rb" + [class]{MaxHeap}-[func]{push} + + [class]{MaxHeap}-[func]{sift_up} + ``` + === "Zig" ```zig title="my_heap.zig" @@ -1703,6 +1764,14 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="my_heap.rb" + [class]{MaxHeap}-[func]{pop} + + [class]{MaxHeap}-[func]{sift_down} + ``` + === "Zig" ```zig title="my_heap.zig" diff --git a/docs/chapter_heap/top_k.md b/docs/chapter_heap/top_k.md index ca4e54a67..f32a7674d 100644 --- a/docs/chapter_heap/top_k.md +++ b/docs/chapter_heap/top_k.md @@ -434,6 +434,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="top_k.rb" + [class]{}-[func]{top_k_heap} + ``` + === "Zig" ```zig title="top_k.zig" diff --git a/docs/chapter_preface/suggestions.md b/docs/chapter_preface/suggestions.md index ee744419c..ba7444244 100644 --- a/docs/chapter_preface/suggestions.md +++ b/docs/chapter_preface/suggestions.md @@ -173,6 +173,12 @@ comments: true */ ``` +=== "Ruby" + + ```ruby title="" + + ``` + === "Zig" ```zig title="" diff --git a/docs/chapter_searching/binary_search.md b/docs/chapter_searching/binary_search.md index bad38bad4..b738b6ad0 100755 --- a/docs/chapter_searching/binary_search.md +++ b/docs/chapter_searching/binary_search.md @@ -336,6 +336,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="binary_search.rb" + [class]{}-[func]{binary_search} + ``` + === "Zig" ```zig title="binary_search.zig" @@ -658,6 +664,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="binary_search.rb" + [class]{}-[func]{binary_search_lcro} + ``` + === "Zig" ```zig title="binary_search.zig" diff --git a/docs/chapter_searching/binary_search_edge.md b/docs/chapter_searching/binary_search_edge.md index adcb02c26..8cd9c96c5 100644 --- a/docs/chapter_searching/binary_search_edge.md +++ b/docs/chapter_searching/binary_search_edge.md @@ -209,6 +209,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="binary_search_edge.rb" + [class]{}-[func]{binary_search_left_edge} + ``` + === "Zig" ```zig title="binary_search_edge.zig" @@ -452,6 +458,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="binary_search_edge.rb" + [class]{}-[func]{binary_search_right_edge} + ``` + === "Zig" ```zig title="binary_search_edge.zig" diff --git a/docs/chapter_searching/binary_search_insertion.md b/docs/chapter_searching/binary_search_insertion.md index 91dcf6c04..0d14a0eec 100644 --- a/docs/chapter_searching/binary_search_insertion.md +++ b/docs/chapter_searching/binary_search_insertion.md @@ -290,6 +290,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="binary_search_insertion.rb" + [class]{}-[func]{binary_search_insertion_simple} + ``` + === "Zig" ```zig title="binary_search_insertion.zig" @@ -616,6 +622,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="binary_search_insertion.rb" + [class]{}-[func]{binary_search_insertion} + ``` + === "Zig" ```zig title="binary_search_insertion.zig" diff --git a/docs/chapter_searching/replace_linear_by_hashing.md b/docs/chapter_searching/replace_linear_by_hashing.md index c179231e2..a06d95962 100755 --- a/docs/chapter_searching/replace_linear_by_hashing.md +++ b/docs/chapter_searching/replace_linear_by_hashing.md @@ -225,6 +225,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="two_sum.rb" + [class]{}-[func]{two_sum_brute_force} + ``` + === "Zig" ```zig title="two_sum.zig" @@ -522,6 +528,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="two_sum.rb" + [class]{}-[func]{two_sum_hash_table} + ``` + === "Zig" ```zig title="two_sum.zig" diff --git a/docs/chapter_sorting/bubble_sort.md b/docs/chapter_sorting/bubble_sort.md index aed3921d8..2718d6f6a 100755 --- a/docs/chapter_sorting/bubble_sort.md +++ b/docs/chapter_sorting/bubble_sort.md @@ -271,6 +271,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="bubble_sort.rb" + [class]{}-[func]{bubble_sort} + ``` + === "Zig" ```zig title="bubble_sort.zig" @@ -574,6 +580,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="bubble_sort.rb" + [class]{}-[func]{bubble_sort_with_flag} + ``` + === "Zig" ```zig title="bubble_sort.zig" diff --git a/docs/chapter_sorting/bucket_sort.md b/docs/chapter_sorting/bucket_sort.md index 318e3738c..a2cfa5bc1 100644 --- a/docs/chapter_sorting/bucket_sort.md +++ b/docs/chapter_sorting/bucket_sort.md @@ -345,8 +345,8 @@ comments: true int k = size / 2; float **buckets = calloc(k, sizeof(float *)); for (int i = 0; i < k; i++) { - // 每个桶最多可以分配 k 个元素 - buckets[i] = calloc(ARRAY_SIZE, sizeof(float)); + // 每个桶最多可以分配 size 个元素 + buckets[i] = calloc(size, sizeof(float)); } // 1. 将数组元素分配到各个桶中 @@ -359,7 +359,7 @@ comments: true j++; } float temp = nums[i]; - while (j < ARRAY_SIZE && buckets[bucket_idx][j] > 0) { + while (j < size && buckets[bucket_idx][j] > 0) { swap(&temp, &buckets[bucket_idx][j]); j++; } @@ -368,12 +368,12 @@ comments: true // 2. 对各个桶执行排序 for (int i = 0; i < k; i++) { - qsort(buckets[i], ARRAY_SIZE, sizeof(float), compare_float); + qsort(buckets[i], size, sizeof(float), compare_float); } // 3. 遍历桶合并结果 for (int i = 0, j = 0; j < k; j++) { - for (int l = 0; l < ARRAY_SIZE; l++) { + for (int l = 0; l < size; l++) { if (buckets[j][l] > 0) { nums[i++] = buckets[j][l]; } @@ -421,6 +421,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="bucket_sort.rb" + [class]{}-[func]{bucket_sort} + ``` + === "Zig" ```zig title="bucket_sort.zig" diff --git a/docs/chapter_sorting/counting_sort.md b/docs/chapter_sorting/counting_sort.md index a18b19cd1..d19e40a69 100644 --- a/docs/chapter_sorting/counting_sort.md +++ b/docs/chapter_sorting/counting_sort.md @@ -345,6 +345,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="counting_sort.rb" + [class]{}-[func]{counting_sort_naive} + ``` + === "Zig" ```zig title="counting_sort.zig" @@ -845,6 +851,12 @@ $$ } ``` +=== "Ruby" + + ```ruby title="counting_sort.rb" + [class]{}-[func]{counting_sort} + ``` + === "Zig" ```zig title="counting_sort.zig" diff --git a/docs/chapter_sorting/heap_sort.md b/docs/chapter_sorting/heap_sort.md index cf38ce7d4..4bea51fd8 100644 --- a/docs/chapter_sorting/heap_sort.md +++ b/docs/chapter_sorting/heap_sort.md @@ -572,6 +572,14 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="heap_sort.rb" + [class]{}-[func]{sift_down} + + [class]{}-[func]{heap_sort} + ``` + === "Zig" ```zig title="heap_sort.zig" diff --git a/docs/chapter_sorting/insertion_sort.md b/docs/chapter_sorting/insertion_sort.md index 44c33c237..3fefbfd3b 100755 --- a/docs/chapter_sorting/insertion_sort.md +++ b/docs/chapter_sorting/insertion_sort.md @@ -250,6 +250,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="insertion_sort.rb" + [class]{}-[func]{insertion_sort} + ``` + === "Zig" ```zig title="insertion_sort.zig" diff --git a/docs/chapter_sorting/merge_sort.md b/docs/chapter_sorting/merge_sort.md index 542e7b875..7d5737177 100755 --- a/docs/chapter_sorting/merge_sort.md +++ b/docs/chapter_sorting/merge_sort.md @@ -625,6 +625,14 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="merge_sort.rb" + [class]{}-[func]{merge} + + [class]{}-[func]{merge_sort} + ``` + === "Zig" ```zig title="merge_sort.zig" diff --git a/docs/chapter_sorting/quick_sort.md b/docs/chapter_sorting/quick_sort.md index a191de793..9451c1049 100755 --- a/docs/chapter_sorting/quick_sort.md +++ b/docs/chapter_sorting/quick_sort.md @@ -351,6 +351,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="quick_sort.rb" + [class]{QuickSort}-[func]{partition} + ``` + === "Zig" ```zig title="quick_sort.zig" @@ -609,6 +615,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="quick_sort.rb" + [class]{QuickSort}-[func]{quick_sort} + ``` + === "Zig" ```zig title="quick_sort.zig" @@ -1073,6 +1085,14 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="quick_sort.rb" + [class]{QuickSortMedian}-[func]{median_three} + + [class]{QuickSortMedian}-[func]{partition} + ``` + === "Zig" ```zig title="quick_sort.zig" @@ -1364,6 +1384,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="quick_sort.rb" + [class]{QuickSortTailCall}-[func]{quick_sort} + ``` + === "Zig" ```zig title="quick_sort.zig" diff --git a/docs/chapter_sorting/radix_sort.md b/docs/chapter_sorting/radix_sort.md index 53e534291..f4d13362e 100644 --- a/docs/chapter_sorting/radix_sort.md +++ b/docs/chapter_sorting/radix_sort.md @@ -672,6 +672,16 @@ $$ } ``` +=== "Ruby" + + ```ruby title="radix_sort.rb" + [class]{}-[func]{digit} + + [class]{}-[func]{counting_sort_digit} + + [class]{}-[func]{radix_sort} + ``` + === "Zig" ```zig title="radix_sort.zig" diff --git a/docs/chapter_sorting/selection_sort.md b/docs/chapter_sorting/selection_sort.md index 5a35ac2e9..56efd2cb2 100644 --- a/docs/chapter_sorting/selection_sort.md +++ b/docs/chapter_sorting/selection_sort.md @@ -300,6 +300,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="selection_sort.rb" + [class]{}-[func]{selection_sort} + ``` + === "Zig" ```zig title="selection_sort.zig" diff --git a/docs/chapter_stack_and_queue/deque.md b/docs/chapter_stack_and_queue/deque.md index b34307606..96916f466 100644 --- a/docs/chapter_stack_and_queue/deque.md +++ b/docs/chapter_stack_and_queue/deque.md @@ -293,7 +293,7 @@ comments: true int size = deque.length; /* 判断双向队列是否为空 */ - bool isEmpty = deque.isEmpty;W + bool isEmpty = deque.isEmpty; ``` === "Rust" @@ -337,6 +337,34 @@ comments: true === "Kotlin" ```kotlin title="deque.kt" + /* 初始化双向队列 */ + val deque = LinkedList() + + /* 元素入队 */ + deque.offerLast(2) // 添加至队尾 + deque.offerLast(5) + deque.offerLast(4) + deque.offerFirst(3) // 添加至队首 + deque.offerFirst(1) + + /* 访问元素 */ + val peekFirst = deque.peekFirst() // 队首元素 + val peekLast = deque.peekLast() // 队尾元素 + + /* 元素出队 */ + val popFirst = deque.pollFirst() // 队首元素出队 + val popLast = deque.pollLast() // 队尾元素出队 + + /* 获取双向队列的长度 */ + val size = deque.size + + /* 判断双向队列是否为空 */ + val isEmpty = deque.isEmpty() + ``` + +=== "Ruby" + + ```ruby title="deque.rb" ``` @@ -1969,6 +1997,14 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="linkedlist_deque.rb" + [class]{ListNode}-[func]{} + + [class]{LinkedListDeque}-[func]{} + ``` + === "Zig" ```zig title="linkedlist_deque.zig" @@ -2635,7 +2671,7 @@ comments: true } // 计算队尾指针,指向队尾索引 + 1 rear := q.index(q.front + q.queSize) - // 将 num 添加至队首 + // 将 num 添加至队尾 q.nums[rear] = num q.queSize++ } @@ -3451,6 +3487,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="array_deque.rb" + [class]{ArrayDeque}-[func]{} + ``` + === "Zig" ```zig title="array_deque.zig" diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index eca7879b3..c904f7d9e 100755 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -315,6 +315,32 @@ comments: true === "Kotlin" ```kotlin title="queue.kt" + /* 初始化队列 */ + val queue = LinkedList() + + /* 元素入队 */ + queue.offer(1) + queue.offer(3) + queue.offer(2) + queue.offer(5) + queue.offer(4) + + /* 访问队首元素 */ + val peek = queue.peek() + + /* 元素出队 */ + val pop = queue.poll() + + /* 获取队列的长度 */ + val size = queue.size + + /* 判断队列是否为空 */ + val isEmpty = queue.isEmpty() + ``` + +=== "Ruby" + + ```ruby title="queue.rb" ``` @@ -1196,6 +1222,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="linkedlist_queue.rb" + [class]{LinkedListQueue}-[func]{} + ``` + === "Zig" ```zig title="linkedlist_queue.zig" @@ -2176,6 +2208,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="array_queue.rb" + [class]{ArrayQueue}-[func]{} + ``` + === "Zig" ```zig title="array_queue.zig" diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 1c956994a..e3f773d86 100755 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -309,6 +309,32 @@ comments: true === "Kotlin" ```kotlin title="stack.kt" + /* 初始化栈 */ + val stack = Stack() + + /* 元素入栈 */ + stack.push(1) + stack.push(3) + stack.push(2) + stack.push(5) + stack.push(4) + + /* 访问栈顶元素 */ + val peek = stack.peek() + + /* 元素出栈 */ + val pop = stack.pop() + + /* 获取栈的长度 */ + val size = stack.size + + /* 判断是否为空 */ + val isEmpty = stack.isEmpty() + ``` + +=== "Ruby" + + ```ruby title="stack.rb" ``` @@ -1068,6 +1094,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="linkedlist_stack.rb" + [class]{LinkedListStack}-[func]{} + ``` + === "Zig" ```zig title="linkedlist_stack.zig" @@ -1621,10 +1653,7 @@ comments: true /* 出栈 */ fn pop(&mut self) -> Option { - match self.stack.pop() { - Some(num) => Some(num), - None => None, - } + self.stack.pop() } /* 访问栈顶元素 */ @@ -1745,6 +1774,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="array_stack.rb" + [class]{ArrayStack}-[func]{} + ``` + === "Zig" ```zig title="array_stack.zig" diff --git a/docs/chapter_tree/array_representation_of_tree.md b/docs/chapter_tree/array_representation_of_tree.md index 89b3d909b..99fcbffc6 100644 --- a/docs/chapter_tree/array_representation_of_tree.md +++ b/docs/chapter_tree/array_representation_of_tree.md @@ -123,6 +123,14 @@ comments: true === "Kotlin" ```kotlin title="" + /* 二叉树的数组表示 */ + // 使用 null 来表示空位 + val tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 ) + ``` + +=== "Ruby" + + ```ruby title="" ``` @@ -1240,6 +1248,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="array_binary_tree.rb" + [class]{ArrayBinaryTree}-[func]{} + ``` + === "Zig" ```zig title="array_binary_tree.zig" diff --git a/docs/chapter_tree/avl_tree.md b/docs/chapter_tree/avl_tree.md index 06d26d2d1..90df15a39 100644 --- a/docs/chapter_tree/avl_tree.md +++ b/docs/chapter_tree/avl_tree.md @@ -211,6 +211,17 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二 === "Kotlin" ```kotlin title="" + /* AVL 树节点类 */ + class TreeNode(val _val: Int) { // 节点值 + val height: Int = 0 // 节点高度 + val left: TreeNode? = null // 左子节点 + val right: TreeNode? = null // 右子节点 + } + ``` + +=== "Ruby" + + ```ruby title="" ``` @@ -441,6 +452,14 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二 } ``` +=== "Ruby" + + ```ruby title="avl_tree.rb" + [class]{AVLTree}-[func]{height} + + [class]{AVLTree}-[func]{update_height} + ``` + === "Zig" ```zig title="avl_tree.zig" @@ -616,6 +635,12 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二 } ``` +=== "Ruby" + + ```ruby title="avl_tree.rb" + [class]{AVLTree}-[func]{balance_factor} + ``` + === "Zig" ```zig title="avl_tree.zig" @@ -885,6 +910,12 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中 } ``` +=== "Ruby" + + ```ruby title="avl_tree.rb" + [class]{AVLTree}-[func]{right_rotate} + ``` + === "Zig" ```zig title="avl_tree.zig" @@ -1140,6 +1171,12 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中 } ``` +=== "Ruby" + + ```ruby title="avl_tree.rb" + [class]{AVLTree}-[func]{left_rotate} + ``` + === "Zig" ```zig title="avl_tree.zig" @@ -1608,6 +1645,12 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中 } ``` +=== "Ruby" + + ```ruby title="avl_tree.rb" + [class]{AVLTree}-[func]{rotate} + ``` + === "Zig" ```zig title="avl_tree.zig" @@ -1991,6 +2034,14 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区 } ``` +=== "Ruby" + + ```ruby title="avl_tree.rb" + [class]{AVLTree}-[func]{insert} + + [class]{AVLTree}-[func]{insert_helper} + ``` + === "Zig" ```zig title="avl_tree.zig" @@ -2576,6 +2627,14 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区 } ``` +=== "Ruby" + + ```ruby title="avl_tree.rb" + [class]{AVLTree}-[func]{remove} + + [class]{AVLTree}-[func]{remove_helper} + ``` + === "Zig" ```zig title="avl_tree.zig" diff --git a/docs/chapter_tree/binary_search_tree.md b/docs/chapter_tree/binary_search_tree.md index b39545533..fc6e83eff 100755 --- a/docs/chapter_tree/binary_search_tree.md +++ b/docs/chapter_tree/binary_search_tree.md @@ -310,6 +310,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="binary_search_tree.rb" + [class]{BinarySearchTree}-[func]{search} + ``` + === "Zig" ```zig title="binary_search_tree.zig" @@ -756,6 +762,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="binary_search_tree.rb" + [class]{BinarySearchTree}-[func]{insert} + ``` + === "Zig" ```zig title="binary_search_tree.zig" @@ -1512,6 +1524,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="binary_search_tree.rb" + [class]{BinarySearchTree}-[func]{remove} + ``` + === "Zig" ```zig title="binary_search_tree.zig" diff --git a/docs/chapter_tree/binary_tree.md b/docs/chapter_tree/binary_tree.md index 2fc54475f..556a3b892 100644 --- a/docs/chapter_tree/binary_tree.md +++ b/docs/chapter_tree/binary_tree.md @@ -183,6 +183,16 @@ comments: true === "Kotlin" ```kotlin title="" + /* 二叉树节点类 */ + class TreeNode(val _val: Int) { // 节点值 + val left: TreeNode? = null // 左子节点引用 + val right: TreeNode? = null // 右子节点引用 + } + ``` + +=== "Ruby" + + ```ruby title="" ``` @@ -414,6 +424,22 @@ comments: true === "Kotlin" ```kotlin title="binary_tree.kt" + // 初始化节点 + val n1 = TreeNode(1) + val n2 = TreeNode(2) + val n3 = TreeNode(3) + val n4 = TreeNode(4) + val n5 = TreeNode(5) + // 构建节点之间的引用(指针) + n1.left = n2 + n1.right = n3 + n2.left = n4 + n2.right = n5 + ``` + +=== "Ruby" + + ```ruby title="binary_tree.rb" ``` @@ -568,6 +594,17 @@ comments: true === "Kotlin" ```kotlin title="binary_tree.kt" + val P = TreeNode(0) + // 在 n1 -> n2 中间插入节点 P + n1.left = P + P.left = n2 + // 删除节点 P + n1.left = n2 + ``` + +=== "Ruby" + + ```ruby title="binary_tree.rb" ``` diff --git a/docs/chapter_tree/binary_tree_traversal.md b/docs/chapter_tree/binary_tree_traversal.md index 057c0d659..6f36b275d 100755 --- a/docs/chapter_tree/binary_tree_traversal.md +++ b/docs/chapter_tree/binary_tree_traversal.md @@ -314,6 +314,12 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="binary_tree_bfs.rb" + [class]{}-[func]{level_order} + ``` + === "Zig" ```zig title="binary_tree_bfs.zig" @@ -781,6 +787,16 @@ comments: true } ``` +=== "Ruby" + + ```ruby title="binary_tree_dfs.rb" + [class]{}-[func]{pre_order} + + [class]{}-[func]{in_order} + + [class]{}-[func]{post_order} + ``` + === "Zig" ```zig title="binary_tree_dfs.zig" diff --git a/en/docs/chapter_array_and_linkedlist/array.md b/en/docs/chapter_array_and_linkedlist/array.md index add27422b..31544e4b2 100755 --- a/en/docs/chapter_array_and_linkedlist/array.md +++ b/en/docs/chapter_array_and_linkedlist/array.md @@ -293,6 +293,19 @@ Accessing elements in an array is highly efficient, allowing us to randomly acce } ``` +=== "Ruby" + + ```ruby title="array.rb" + ### 随机访问元素 ### + def random_access(nums) + # 在区间 [0, nums.length) 中随机抽取一个数字 + random_index = Random.rand 0...(nums.length - 1) + + # 获取并返回随机元素 + nums[random_index] + end + ``` + === "Zig" ```zig title="array.zig" @@ -487,6 +500,21 @@ It's important to note that due to the fixed length of an array, inserting an el } ``` +=== "Ruby" + + ```ruby title="array.rb" + ### 在数组的索引 index 处插入元素 num ### + def insert(nums, num, index) + # 把索引 index 以及之后的所有元素向后移动一位 + for i in (nums.length - 1).downto(index + 1) + nums[i] = nums[i - 1] + end + + # 将 num 赋给 index 处的元素 + nums[index] = num + end + ``` + === "Zig" ```zig title="array.zig" @@ -660,6 +688,18 @@ Please note that after deletion, the former last element becomes "meaningless," } ``` +=== "Ruby" + + ```ruby title="array.rb" + ### 删除索引 index 处的元素 ### + def remove(nums, index) + # 把索引 index 之后的所有元素向前移动一位 + for i in index...nums.length + nums[i] = nums[i + 1] || 0 + end + end + ``` + === "Zig" ```zig title="array.zig" @@ -900,6 +940,25 @@ In most programming languages, we can traverse an array either by using indices } ``` +=== "Ruby" + + ```ruby title="array.rb" + ### 遍历数组 ### + def traverse(nums) + count = 0 + + # 通过索引遍历数组 + for i in 0...nums.length + count += nums[i] + end + + # 直接遍历数组元素 + for num in nums + count += num + end + end + ``` + === "Zig" ```zig title="array.zig" @@ -1087,6 +1146,19 @@ Because arrays are linear data structures, this operation is commonly referred t } ``` +=== "Ruby" + + ```ruby title="array.rb" + ### 在数组中查找指定元素 ### + def find(nums, target) + for i in 0...nums.length + return i if nums[i] == target + end + + -1 + end + ``` + === "Zig" ```zig title="array.zig" @@ -1310,6 +1382,26 @@ To expand an array, it's necessary to create a larger array and then copy the e } ``` +=== "Ruby" + + ```ruby title="array.rb" + ### 扩展数组长度 ### + # 请注意,Ruby 的 Array 是动态数组,可以直接扩展 + # 为了方便学习,本函数将 Array 看作长度不可变的数组 + def extend(nums, enlarge) + # 初始化一个扩展长度后的数组 + res = Array.new(nums.length + enlarge, 0) + + # 将原数组中的所有元素复制到新数组 + for i in 0...nums.length + res[i] = nums[i] + end + + # 返回扩展后的新数组 + res + end + ``` + === "Zig" ```zig title="array.zig" diff --git a/en/docs/chapter_array_and_linkedlist/linked_list.md b/en/docs/chapter_array_and_linkedlist/linked_list.md index a55e3b72c..120c333fe 100755 --- a/en/docs/chapter_array_and_linkedlist/linked_list.md +++ b/en/docs/chapter_array_and_linkedlist/linked_list.md @@ -552,6 +552,18 @@ By comparison, inserting an element into an array has a time complexity of $O(n) } ``` +=== "Ruby" + + ```ruby title="linked_list.rb" + ### 在链表的节点 n0 之后插入节点 _p ### + # Ruby 的 `p` 是一个内置函数, `P` 是一个常量,所以可以使用 `_p` 代替 + def insert(n0, _p) + n1 = n0.next + _p.next = n1 + n0.next = _p + end + ``` + === "Zig" ```zig title="linked_list.zig" @@ -752,6 +764,20 @@ It's important to note that even though node `P` continues to point to `n1` afte } ``` +=== "Ruby" + + ```ruby title="linked_list.rb" + ### 删除链表的节点 n0 之后的首个节点 ### + def remove(n0) + return if n0.next.nil? + + # n0 -> remove_node -> n1 + remove_node = n0.next + n1 = remove_node.next + n0.next = n1 + end + ``` + === "Zig" ```zig title="linked_list.zig" @@ -945,6 +971,20 @@ It's important to note that even though node `P` continues to point to `n1` afte } ``` +=== "Ruby" + + ```ruby title="linked_list.rb" + ### 访问链表中索引为 index 的节点 ### + def access(head, index) + for i in 0...index + return nil if head.nil? + head = head.next + end + + head + end + ``` + === "Zig" ```zig title="linked_list.zig" @@ -1164,6 +1204,22 @@ Traverse the linked list to locate a node whose value matches `target`, and then } ``` +=== "Ruby" + + ```ruby title="linked_list.rb" + ### 在链表中查找值为 target 的首个节点 ### + def find(head, target) + index = 0 + while head + return index if head.val == target + head = head.next + index += 1 + end + + -1 + end + ``` + === "Zig" ```zig title="linked_list.zig" diff --git a/en/docs/chapter_array_and_linkedlist/list.md b/en/docs/chapter_array_and_linkedlist/list.md index 4d27fc841..b5a3d31aa 100755 --- a/en/docs/chapter_array_and_linkedlist/list.md +++ b/en/docs/chapter_array_and_linkedlist/list.md @@ -2141,6 +2141,100 @@ To enhance our understanding of how lists work, we will attempt to implement a s } ``` +=== "Ruby" + + ```ruby title="my_list.rb" + ### 列表类 ### + class MyList + attr_reader :size # 获取列表长度(当前元素数量) + attr_reader :capacity # 获取列表容量 + + ### 构造方法 ### + def initialize + @capacity = 10 + @size = 0 + @extend_ratio = 2 + @arr = Array.new capacity + end + + ### 访问元素 ### + def get(index) + # 索引如果越界,则抛出异常,下同 + raise IndexError, "索引越界" if index < 0 || index >= size + @arr[index] + end + + ### 访问元素 ### + def set(index, num) + raise IndexError, "索引越界" if index < 0 || index >= size + @arr[index] = num + end + + ### 在尾部添加元素 ### + def add(num) + # 元素数量超出容量时,触发扩容机制 + extend_capacity if size == capacity + @arr[size] = num + + # 更新元素数量 + @size += 1 + end + + ### 在中间插入元素 ### + def insert(index, num) + raise IndexError, "索引越界" if index < 0 || index >= size + + # 元素数量超出容量时,触发扩容机制 + extend_capacity if size == capacity + + # 将索引 index 以及之后的元素都向后移动一位 + for j in (size - 1).downto(index) + @arr[j + 1] = @arr[j] + end + @arr[index] = num + + # 更新元素数量 + @size += 1 + end + + ### 删除元素 ### + def remove(index) + raise IndexError, "索引越界" if index < 0 || index >= size + num = @arr[index] + + # 将将索引 index 之后的元素都向前移动一位 + for j in index...size + @arr[j] = @arr[j + 1] + end + + # 更新元素数量 + @size -= 1 + + # 返回被删除的元素 + num + end + + ### 列表扩容 ### + def extend_capacity + # 新建一个长度为原数组 extend_ratio 倍的新数组,并将原数组复制到新数组 + arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1)) + # 更新列表容量 + @capacity = arr.length + end + + ### 将列表转换为数组 ### + def to_array + sz = size + # 仅转换有效长度范围内的列表元素 + arr = Array.new sz + for i in 0...sz + arr[i] = get i + end + arr + end + end + ``` + === "Zig" ```zig title="my_list.zig" diff --git a/en/docs/chapter_computational_complexity/iteration_and_recursion.md b/en/docs/chapter_computational_complexity/iteration_and_recursion.md index 4c55673da..9a9ad351f 100644 --- a/en/docs/chapter_computational_complexity/iteration_and_recursion.md +++ b/en/docs/chapter_computational_complexity/iteration_and_recursion.md @@ -182,6 +182,12 @@ The following function uses a `for` loop to perform a summation of $1 + 2 + \dot } ``` +=== "Ruby" + + ```ruby title="iteration.rb" + [class]{}-[func]{for_loop} + ``` + === "Zig" ```zig title="iteration.zig" @@ -408,6 +414,12 @@ Below we use a `while` loop to implement the sum $1 + 2 + \dots + n$. } ``` +=== "Ruby" + + ```ruby title="iteration.rb" + [class]{}-[func]{while_loop} + ``` + === "Zig" ```zig title="iteration.zig" @@ -649,6 +661,12 @@ For example, in the following code, the condition variable $i$ is updated twice } ``` +=== "Ruby" + + ```ruby title="iteration.rb" + [class]{}-[func]{while_loop_ii} + ``` + === "Zig" ```zig title="iteration.zig" @@ -883,6 +901,12 @@ We can nest one loop structure within another. Below is an example using `for` l } ``` +=== "Ruby" + + ```ruby title="iteration.rb" + [class]{}-[func]{nested_for_loop} + ``` + === "Zig" ```zig title="iteration.zig" @@ -1112,6 +1136,12 @@ Observe the following code, where simply calling the function `recur(n)` can com } ``` +=== "Ruby" + + ```ruby title="recursion.rb" + [class]{}-[func]{recur} + ``` + === "Zig" ```zig title="recursion.zig" @@ -1318,8 +1348,9 @@ For example, in calculating $1 + 2 + \dots + n$, we can make the result variable === "Kotlin" ```kotlin title="recursion.kt" - /* Kotlin tailrec 关键词使函数实现尾递归优化 */ + /* 尾递归 */ tailrec fun tailRecur(n: Int, res: Int): Int { + // 添加 tailrec 关键词,以开启尾递归优化 // 终止条件 if (n == 0) return res @@ -1328,6 +1359,12 @@ For example, in calculating $1 + 2 + \dots + n$, we can make the result variable } ``` +=== "Ruby" + + ```ruby title="recursion.rb" + [class]{}-[func]{tail_recur} + ``` + === "Zig" ```zig title="recursion.zig" @@ -1554,6 +1591,12 @@ Using the recursive relation, and considering the first two numbers as terminati } ``` +=== "Ruby" + + ```ruby title="recursion.rb" + [class]{}-[func]{fib} + ``` + === "Zig" ```zig title="recursion.zig" @@ -1890,6 +1933,12 @@ Therefore, **we can use an explicit stack to simulate the behavior of the call s } ``` +=== "Ruby" + + ```ruby title="recursion.rb" + [class]{}-[func]{for_loop_recur} + ``` + === "Zig" ```zig title="recursion.zig" diff --git a/en/docs/chapter_computational_complexity/space_complexity.md b/en/docs/chapter_computational_complexity/space_complexity.md index 86b406c3e..cbfc66583 100644 --- a/en/docs/chapter_computational_complexity/space_complexity.md +++ b/en/docs/chapter_computational_complexity/space_complexity.md @@ -1077,6 +1077,14 @@ Note that memory occupied by initializing variables or calling functions in a lo } ``` +=== "Ruby" + + ```ruby title="space_complexity.rb" + [class]{}-[func]{function} + + [class]{}-[func]{constant} + ``` + === "Zig" ```zig title="space_complexity.zig" @@ -1373,6 +1381,12 @@ Linear order is common in arrays, linked lists, stacks, queues, etc., where the } ``` +=== "Ruby" + + ```ruby title="space_complexity.rb" + [class]{}-[func]{linear} + ``` + === "Zig" ```zig title="space_complexity.zig" @@ -1549,6 +1563,12 @@ As shown below, this function's recursive depth is $n$, meaning there are $n$ in } ``` +=== "Ruby" + + ```ruby title="space_complexity.rb" + [class]{}-[func]{linear_recur} + ``` + === "Zig" ```zig title="space_complexity.zig" @@ -1783,6 +1803,12 @@ Quadratic order is common in matrices and graphs, where the number of elements i } ``` +=== "Ruby" + + ```ruby title="space_complexity.rb" + [class]{}-[func]{quadratic} + ``` + === "Zig" ```zig title="space_complexity.zig" @@ -1972,6 +1998,12 @@ As shown below, the recursive depth of this function is $n$, and in each recursi } ``` +=== "Ruby" + + ```ruby title="space_complexity.rb" + [class]{}-[func]{quadratic_recur} + ``` + === "Zig" ```zig title="space_complexity.zig" @@ -2164,6 +2196,12 @@ Exponential order is common in binary trees. Observe the below image, a "full bi } ``` +=== "Ruby" + + ```ruby title="space_complexity.rb" + [class]{}-[func]{build_tree} + ``` + === "Zig" ```zig title="space_complexity.zig" diff --git a/en/docs/chapter_computational_complexity/time_complexity.md b/en/docs/chapter_computational_complexity/time_complexity.md index a1b996160..c1163f5a1 100644 --- a/en/docs/chapter_computational_complexity/time_complexity.md +++ b/en/docs/chapter_computational_complexity/time_complexity.md @@ -1140,6 +1140,12 @@ Constant order means the number of operations is independent of the input data s } ``` +=== "Ruby" + + ```ruby title="time_complexity.rb" + [class]{}-[func]{constant} + ``` + === "Zig" ```zig title="time_complexity.zig" @@ -1312,6 +1318,12 @@ Linear order indicates the number of operations grows linearly with the input da } ``` +=== "Ruby" + + ```ruby title="time_complexity.rb" + [class]{}-[func]{linear} + ``` + === "Zig" ```zig title="time_complexity.zig" @@ -1499,6 +1511,12 @@ Operations like array traversal and linked list traversal have a time complexity } ``` +=== "Ruby" + + ```ruby title="time_complexity.rb" + [class]{}-[func]{array_traversal} + ``` + === "Zig" ```zig title="time_complexity.zig" @@ -1713,6 +1731,12 @@ Quadratic order means the number of operations grows quadratically with the inpu } ``` +=== "Ruby" + + ```ruby title="time_complexity.rb" + [class]{}-[func]{quadratic} + ``` + === "Zig" ```zig title="time_complexity.zig" @@ -2013,6 +2037,12 @@ For instance, in bubble sort, the outer loop runs $n - 1$ times, and the inner l } ``` +=== "Ruby" + + ```ruby title="time_complexity.rb" + [class]{}-[func]{bubble_sort} + ``` + === "Zig" ```zig title="time_complexity.zig" @@ -2269,6 +2299,12 @@ The following image and code simulate the cell division process, with a time com } ``` +=== "Ruby" + + ```ruby title="time_complexity.rb" + [class]{}-[func]{exponential} + ``` + === "Zig" ```zig title="time_complexity.zig" @@ -2432,6 +2468,12 @@ In practice, exponential order often appears in recursive functions. For example } ``` +=== "Ruby" + + ```ruby title="time_complexity.rb" + [class]{}-[func]{exp_recur} + ``` + === "Zig" ```zig title="time_complexity.zig" @@ -2623,6 +2665,12 @@ The following image and code simulate the "halving each round" process, with a t } ``` +=== "Ruby" + + ```ruby title="time_complexity.rb" + [class]{}-[func]{logarithmic} + ``` + === "Zig" ```zig title="time_complexity.zig" @@ -2780,6 +2828,12 @@ Like exponential order, logarithmic order also frequently appears in recursive f } ``` +=== "Ruby" + + ```ruby title="time_complexity.rb" + [class]{}-[func]{log_recur} + ``` + === "Zig" ```zig title="time_complexity.zig" @@ -2988,6 +3042,12 @@ Linear-logarithmic order often appears in nested loops, with the complexities of } ``` +=== "Ruby" + + ```ruby title="time_complexity.rb" + [class]{}-[func]{linear_log_recur} + ``` + === "Zig" ```zig title="time_complexity.zig" @@ -3214,6 +3274,12 @@ Factorials are typically implemented using recursion. As shown in the image and } ``` +=== "Ruby" + + ```ruby title="time_complexity.rb" + [class]{}-[func]{factorial_recur} + ``` + === "Zig" ```zig title="time_complexity.zig" @@ -3603,6 +3669,14 @@ The "worst-case time complexity" corresponds to the asymptotic upper bound, deno } ``` +=== "Ruby" + + ```ruby title="worst_best_time_complexity.rb" + [class]{}-[func]{random_numbers} + + [class]{}-[func]{find_one} + ``` + === "Zig" ```zig title="worst_best_time_complexity.zig" diff --git a/en/docs/chapter_hashing/hash_algorithm.md b/en/docs/chapter_hashing/hash_algorithm.md index bdfa6a859..8cb0f8a9e 100644 --- a/en/docs/chapter_hashing/hash_algorithm.md +++ b/en/docs/chapter_hashing/hash_algorithm.md @@ -594,6 +594,18 @@ The design of hash algorithms is a complex issue that requires consideration of } ``` +=== "Ruby" + + ```ruby title="simple_hash.rb" + [class]{}-[func]{add_hash} + + [class]{}-[func]{mul_hash} + + [class]{}-[func]{xor_hash} + + [class]{}-[func]{rot_hash} + ``` + === "Zig" ```zig title="simple_hash.zig" diff --git a/en/docs/chapter_hashing/hash_collision.md b/en/docs/chapter_hashing/hash_collision.md index 4b4ce91c2..a9a9eaa60 100644 --- a/en/docs/chapter_hashing/hash_collision.md +++ b/en/docs/chapter_hashing/hash_collision.md @@ -1426,6 +1426,12 @@ The code below provides a simple implementation of a separate chaining hash tabl } ``` +=== "Ruby" + + ```ruby title="hash_map_chaining.rb" + [class]{HashMapChaining}-[func]{} + ``` + === "Zig" ```zig title="hash_map_chaining.zig" @@ -3072,6 +3078,12 @@ The code below implements an open addressing (linear probing) hash table with la } ``` +=== "Ruby" + + ```ruby title="hash_map_open_addressing.rb" + [class]{HashMapOpenAddressing}-[func]{} + ``` + === "Zig" ```zig title="hash_map_open_addressing.zig" diff --git a/en/docs/chapter_hashing/hash_map.md b/en/docs/chapter_hashing/hash_map.md index 6cbe29657..9c31db5bd 100755 --- a/en/docs/chapter_hashing/hash_map.md +++ b/en/docs/chapter_hashing/hash_map.md @@ -1697,6 +1697,14 @@ The following code implements a simple hash table. Here, we encapsulate `key` an } ``` +=== "Ruby" + + ```ruby title="array_hash_map.rb" + [class]{Pair}-[func]{} + + [class]{ArrayHashMap}-[func]{} + ``` + === "Zig" ```zig title="array_hash_map.zig" diff --git a/en/docs/chapter_stack_and_queue/deque.md b/en/docs/chapter_stack_and_queue/deque.md index 2badf8159..5238a0b05 100644 --- a/en/docs/chapter_stack_and_queue/deque.md +++ b/en/docs/chapter_stack_and_queue/deque.md @@ -1968,6 +1968,14 @@ The implementation code is as follows: } ``` +=== "Ruby" + + ```ruby title="linkedlist_deque.rb" + [class]{ListNode}-[func]{} + + [class]{LinkedListDeque}-[func]{} + ``` + === "Zig" ```zig title="linkedlist_deque.zig" @@ -2634,7 +2642,7 @@ The implementation only needs to add methods for "front enqueue" and "rear deque } // 计算队尾指针,指向队尾索引 + 1 rear := q.index(q.front + q.queSize) - // 将 num 添加至队首 + // 将 num 添加至队尾 q.nums[rear] = num q.queSize++ } @@ -3450,6 +3458,12 @@ The implementation only needs to add methods for "front enqueue" and "rear deque } ``` +=== "Ruby" + + ```ruby title="array_deque.rb" + [class]{ArrayDeque}-[func]{} + ``` + === "Zig" ```zig title="array_deque.zig" diff --git a/en/docs/chapter_stack_and_queue/queue.md b/en/docs/chapter_stack_and_queue/queue.md index 0a62b4dd5..c90075361 100755 --- a/en/docs/chapter_stack_and_queue/queue.md +++ b/en/docs/chapter_stack_and_queue/queue.md @@ -1196,6 +1196,12 @@ Below is the code for implementing a queue using a linked list: } ``` +=== "Ruby" + + ```ruby title="linkedlist_queue.rb" + [class]{LinkedListQueue}-[func]{} + ``` + === "Zig" ```zig title="linkedlist_queue.zig" @@ -2176,6 +2182,12 @@ In a circular array, `front` or `rear` needs to loop back to the start of the ar } ``` +=== "Ruby" + + ```ruby title="array_queue.rb" + [class]{ArrayQueue}-[func]{} + ``` + === "Zig" ```zig title="array_queue.zig" diff --git a/en/docs/chapter_stack_and_queue/stack.md b/en/docs/chapter_stack_and_queue/stack.md index 4f028c3ff..4a95910c9 100755 --- a/en/docs/chapter_stack_and_queue/stack.md +++ b/en/docs/chapter_stack_and_queue/stack.md @@ -1068,6 +1068,12 @@ Below is an example code for implementing a stack based on a linked list: } ``` +=== "Ruby" + + ```ruby title="linkedlist_stack.rb" + [class]{LinkedListStack}-[func]{} + ``` + === "Zig" ```zig title="linkedlist_stack.zig" @@ -1621,10 +1627,7 @@ Since the elements to be pushed onto the stack may continuously increase, we can /* 出栈 */ fn pop(&mut self) -> Option { - match self.stack.pop() { - Some(num) => Some(num), - None => None, - } + self.stack.pop() } /* 访问栈顶元素 */ @@ -1745,6 +1748,12 @@ Since the elements to be pushed onto the stack may continuously increase, we can } ``` +=== "Ruby" + + ```ruby title="array_stack.rb" + [class]{ArrayStack}-[func]{} + ``` + === "Zig" ```zig title="array_stack.zig" diff --git a/overrides/stylesheets/extra.css b/overrides/stylesheets/extra.css index 67f0d38d2..84da9d4e2 100644 --- a/overrides/stylesheets/extra.css +++ b/overrides/stylesheets/extra.css @@ -209,7 +209,7 @@ body { /* code block tabs */ .md-typeset .tabbed-labels>label { - font-size: 0.59rem; + font-size: 0.545rem; } /* header banner */