This commit is contained in:
krahets 2024-03-31 03:53:04 +08:00
parent 87af663929
commit c23e576da4
68 changed files with 2139 additions and 22 deletions

View file

@ -112,7 +112,7 @@ comments: true
| adjacency list | 邻接表 | 鄰接表 | | adjacency list | 邻接表 | 鄰接表 |
| breadth-first search | 广度优先搜索 | 廣度優先搜尋 | | breadth-first search | 广度优先搜索 | 廣度優先搜尋 |
| depth-first search | 深度优先搜索 | 深度優先搜尋 | | depth-first search | 深度优先搜索 | 深度優先搜尋 |
| binary search | 二分查找 | 二分查找 | | binary search | 二分查找 | 二分搜尋 |
| searching algorithm | 搜索算法 | 搜尋演算法 | | searching algorithm | 搜索算法 | 搜尋演算法 |
| sorting algorithm | 排序算法 | 排序演算法 | | sorting algorithm | 排序算法 | 排序演算法 |
| selection sort | 选择排序 | 選擇排序 | | selection sort | 选择排序 | 選擇排序 |

View file

@ -114,7 +114,17 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="array.kt" ```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" === "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"
```zig title="array.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"
```zig title="array.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"
```zig title="array.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"
```zig title="array.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"
```zig title="array.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"
```zig title="array.zig" ```zig title="array.zig"

View file

@ -168,7 +168,27 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="" ```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" === "Zig"
@ -388,7 +408,35 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="linked_list.kt" ```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" === "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"
```zig title="linked_list.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"
```zig title="linked_list.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"
```zig title="linked_list.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"
```zig title="linked_list.zig" ```zig title="linked_list.zig"
@ -1389,7 +1493,30 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="" ```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" === "Zig"

View file

@ -133,7 +133,22 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="list.kt" ```kotlin title="list.kt"
/* 初始化列表 */
// 无初始值
var nums1 = listOf<Int>()
// 有初始值
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" === "Zig"
@ -262,7 +277,19 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="list.kt" ```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" === "Zig"
@ -493,7 +520,41 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="list.kt" ```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" === "Zig"
@ -690,7 +751,32 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="list.kt" ```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" === "Zig"
@ -809,7 +895,17 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="list.kt" ```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" === "Zig"
@ -910,7 +1006,15 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="list.kt" ```kotlin title="list.kt"
/* 排序列表 */
nums.sort() // 排序后,列表元素从小到大排列
```
=== "Ruby"
```ruby title="list.rb"
# 排序列表
nums = nums.sort { |a, b| a <=> b }
``` ```
=== "Zig" === "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"
```zig title="my_list.zig" ```zig title="my_list.zig"

View file

@ -217,6 +217,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="preorder_traversal_i_compact.rb"
[class]{}-[func]{pre_order}
```
=== "Zig" === "Zig"
```zig title="preorder_traversal_i_compact.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"
```zig title="preorder_traversal_ii_compact.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"
```zig title="preorder_traversal_iii_compact.zig" ```zig title="preorder_traversal_iii_compact.zig"
@ -1159,6 +1177,32 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
/* 回溯算法框架 */
fun backtrack(state: State?, choices: List<Choice?>, res: List<State?>?) {
// 判断是否为解
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"
```zig title="preorder_traversal_iii_template.zig" ```zig title="preorder_traversal_iii_template.zig"

View file

@ -706,6 +706,14 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="n_queens.rb"
[class]{}-[func]{backtrack}
[class]{}-[func]{n_queens}
```
=== "Zig" === "Zig"
```zig title="n_queens.zig" ```zig title="n_queens.zig"

View file

@ -503,6 +503,14 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="permutations_i.rb"
[class]{}-[func]{backtrack}
[class]{}-[func]{permutations_i}
```
=== "Zig" === "Zig"
```zig title="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"
```zig title="permutations_ii.zig" ```zig title="permutations_ii.zig"

View file

@ -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"
```zig title="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"
```zig title="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"
```zig title="subset_sum_ii.zig" ```zig title="subset_sum_ii.zig"

View file

@ -182,6 +182,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="iteration.rb"
[class]{}-[func]{for_loop}
```
=== "Zig" === "Zig"
```zig title="iteration.zig" ```zig title="iteration.zig"
@ -408,6 +414,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="iteration.rb"
[class]{}-[func]{while_loop}
```
=== "Zig" === "Zig"
```zig title="iteration.zig" ```zig title="iteration.zig"
@ -649,6 +661,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="iteration.rb"
[class]{}-[func]{while_loop_ii}
```
=== "Zig" === "Zig"
```zig title="iteration.zig" ```zig title="iteration.zig"
@ -883,6 +901,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="iteration.rb"
[class]{}-[func]{nested_for_loop}
```
=== "Zig" === "Zig"
```zig title="iteration.zig" ```zig title="iteration.zig"
@ -1112,6 +1136,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="recursion.rb"
[class]{}-[func]{recur}
```
=== "Zig" === "Zig"
```zig title="recursion.zig" ```zig title="recursion.zig"
@ -1318,8 +1348,9 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="recursion.kt" ```kotlin title="recursion.kt"
/* Kotlin tailrec 关键词使函数实现尾递归优化 */ /* 尾递归 */
tailrec fun tailRecur(n: Int, res: Int): Int { tailrec fun tailRecur(n: Int, res: Int): Int {
// 添加 tailrec 关键词,以开启尾递归优化
// 终止条件 // 终止条件
if (n == 0) if (n == 0)
return res return res
@ -1328,6 +1359,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="recursion.rb"
[class]{}-[func]{tail_recur}
```
=== "Zig" === "Zig"
```zig title="recursion.zig" ```zig title="recursion.zig"
@ -1554,6 +1591,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="recursion.rb"
[class]{}-[func]{fib}
```
=== "Zig" === "Zig"
```zig title="recursion.zig" ```zig title="recursion.zig"
@ -1890,6 +1933,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="recursion.rb"
[class]{}-[func]{for_loop_recur}
```
=== "Zig" === "Zig"
```zig title="recursion.zig" ```zig title="recursion.zig"

View file

@ -318,6 +318,29 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="" ```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"
```kotlin title="" ```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) */ /* 递归 O(n) */
void recur(n: i32) { fn recur(n: i32) {
if n == 1 { if n == 1 {
return; return;
} }
@ -714,6 +749,26 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
fun function(): Int {
// 执行某些操作
return 0
}
/* 循环 O(1) */
fun loop(n: Int) {
for (i in 0..<n) {
function()
}
}
/* 递归 O(n) */
fun recur(n: Int) {
if (n == 1) return
return recur(n - 1)
}
```
=== "Ruby"
```ruby title=""
``` ```
@ -1076,6 +1131,14 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="space_complexity.rb"
[class]{}-[func]{function}
[class]{}-[func]{constant}
```
=== "Zig" === "Zig"
```zig title="space_complexity.zig" ```zig title="space_complexity.zig"
@ -1372,6 +1435,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="space_complexity.rb"
[class]{}-[func]{linear}
```
=== "Zig" === "Zig"
```zig title="space_complexity.zig" ```zig title="space_complexity.zig"
@ -1548,6 +1617,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="space_complexity.rb"
[class]{}-[func]{linear_recur}
```
=== "Zig" === "Zig"
```zig title="space_complexity.zig" ```zig title="space_complexity.zig"
@ -1782,6 +1857,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="space_complexity.rb"
[class]{}-[func]{quadratic}
```
=== "Zig" === "Zig"
```zig title="space_complexity.zig" ```zig title="space_complexity.zig"
@ -1971,6 +2052,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="space_complexity.rb"
[class]{}-[func]{quadratic_recur}
```
=== "Zig" === "Zig"
```zig title="space_complexity.zig" ```zig title="space_complexity.zig"
@ -2163,6 +2250,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="space_complexity.rb"
[class]{}-[func]{build_tree}
```
=== "Zig" === "Zig"
```zig title="space_complexity.zig" ```zig title="space_complexity.zig"

View file

@ -178,6 +178,21 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
// 在某运行平台下
fun algorithm(n: Int) {
var a = 2 // 1 ns
a = a + 1 // 1 ns
a = a * 2 // 10 ns
// 循环 n 次
for (i in 0..<n) { // 1 ns 每轮都要执行 i++
println(0) // 5 ns
}
}
```
=== "Ruby"
```ruby title=""
``` ```
@ -442,6 +457,27 @@ $$
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
// 算法 A 的时间复杂度:常数阶
fun algoritm_A(n: Int) {
println(0)
}
// 算法 B 的时间复杂度:线性阶
fun algorithm_B(n: Int) {
for (i in 0..<n){
println(0)
}
}
// 算法 C 的时间复杂度:常数阶
fun algorithm_C(n: Int) {
for (i in 0..<1000000) {
println(0)
}
}
```
=== "Ruby"
```ruby title=""
``` ```
@ -644,6 +680,20 @@ $$
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
fun algorithm(n: Int) {
var a = 1 // +1
a = a + 1 // +1
a = a * 2 // +1
// 循环 n 次
for (i in 0..<n) { // +1每轮都执行 i ++
println(0) // +1
}
}
```
=== "Ruby"
```ruby title=""
``` ```
@ -909,6 +959,25 @@ $T(n)$ 是一次函数,说明其运行时间的增长趋势是线性的,因
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
fun algorithm(n: Int) {
var a = 1 // +0技巧 1
a = a + n // +0技巧 1
// +n技巧 2
for (i in 0..<5 * n + 1) {
println(0)
}
// +n*n技巧 3
for (i in 0..<2 * n) {
for (j in 0..<n + 1) {
println(0)
}
}
}
```
=== "Ruby"
```ruby title=""
``` ```
@ -1144,6 +1213,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="time_complexity.rb"
[class]{}-[func]{constant}
```
=== "Zig" === "Zig"
```zig title="time_complexity.zig" ```zig title="time_complexity.zig"
@ -1316,6 +1391,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="time_complexity.rb"
[class]{}-[func]{linear}
```
=== "Zig" === "Zig"
```zig title="time_complexity.zig" ```zig title="time_complexity.zig"
@ -1503,6 +1584,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="time_complexity.rb"
[class]{}-[func]{array_traversal}
```
=== "Zig" === "Zig"
```zig title="time_complexity.zig" ```zig title="time_complexity.zig"
@ -1717,6 +1804,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="time_complexity.rb"
[class]{}-[func]{quadratic}
```
=== "Zig" === "Zig"
```zig title="time_complexity.zig" ```zig title="time_complexity.zig"
@ -2017,6 +2110,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="time_complexity.rb"
[class]{}-[func]{bubble_sort}
```
=== "Zig" === "Zig"
```zig title="time_complexity.zig" ```zig title="time_complexity.zig"
@ -2273,6 +2372,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="time_complexity.rb"
[class]{}-[func]{exponential}
```
=== "Zig" === "Zig"
```zig title="time_complexity.zig" ```zig title="time_complexity.zig"
@ -2436,6 +2541,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="time_complexity.rb"
[class]{}-[func]{exp_recur}
```
=== "Zig" === "Zig"
```zig title="time_complexity.zig" ```zig title="time_complexity.zig"
@ -2627,6 +2738,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="time_complexity.rb"
[class]{}-[func]{logarithmic}
```
=== "Zig" === "Zig"
```zig title="time_complexity.zig" ```zig title="time_complexity.zig"
@ -2784,6 +2901,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="time_complexity.rb"
[class]{}-[func]{log_recur}
```
=== "Zig" === "Zig"
```zig title="time_complexity.zig" ```zig title="time_complexity.zig"
@ -2992,6 +3115,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="time_complexity.rb"
[class]{}-[func]{linear_log_recur}
```
=== "Zig" === "Zig"
```zig title="time_complexity.zig" ```zig title="time_complexity.zig"
@ -3218,6 +3347,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="time_complexity.rb"
[class]{}-[func]{factorial_recur}
```
=== "Zig" === "Zig"
```zig title="time_complexity.zig" ```zig title="time_complexity.zig"
@ -3607,6 +3742,14 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="worst_best_time_complexity.rb"
[class]{}-[func]{random_numbers}
[class]{}-[func]{find_one}
```
=== "Zig" === "Zig"
```zig title="worst_best_time_complexity.zig" ```zig title="worst_best_time_complexity.zig"

View file

@ -164,6 +164,16 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="" ```kotlin title=""
// 使用多种基本数据类型来初始化数组
val numbers = IntArray(5)
val decinals = FloatArray(5)
val characters = CharArray(5)
val bools = BooleanArray(5)
```
=== "Ruby"
```ruby title=""
``` ```

View file

@ -419,6 +419,14 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="binary_search_recur.rb"
[class]{}-[func]{dfs}
[class]{}-[func]{binary_search}
```
=== "Zig" === "Zig"
```zig title="binary_search_recur.zig" ```zig title="binary_search_recur.zig"

View file

@ -476,6 +476,14 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="build_tree.rb"
[class]{}-[func]{dfs}
[class]{}-[func]{build_tree}
```
=== "Zig" === "Zig"
```zig title="build_tree.zig" ```zig title="build_tree.zig"

View file

@ -507,6 +507,16 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="hanota.rb"
[class]{}-[func]{move}
[class]{}-[func]{dfs}
[class]{}-[func]{solve_hanota}
```
=== "Zig" === "Zig"
```zig title="hanota.zig" ```zig title="hanota.zig"

View file

@ -301,6 +301,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="min_cost_climbing_stairs_dp.rb"
[class]{}-[func]{min_cost_climbing_stairs_dp}
```
=== "Zig" === "Zig"
```zig title="min_cost_climbing_stairs_dp.zig" ```zig title="min_cost_climbing_stairs_dp.zig"
@ -560,6 +566,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="min_cost_climbing_stairs_dp.rb"
[class]{}-[func]{min_cost_climbing_stairs_dp_comp}
```
=== "Zig" === "Zig"
```zig title="min_cost_climbing_stairs_dp.zig" ```zig title="min_cost_climbing_stairs_dp.zig"
@ -920,6 +932,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="climbing_stairs_constraint_dp.rb"
[class]{}-[func]{climbing_stairs_constraint_dp}
```
=== "Zig" === "Zig"
```zig title="climbing_stairs_constraint_dp.zig" ```zig title="climbing_stairs_constraint_dp.zig"

View file

@ -370,6 +370,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="min_path_sum.rb"
[class]{}-[func]{min_path_sum_dfs}
```
=== "Zig" === "Zig"
```zig title="min_path_sum.zig" ```zig title="min_path_sum.zig"
@ -731,6 +737,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="min_path_sum.rb"
[class]{}-[func]{min_path_sum_dfs_mem}
```
=== "Zig" === "Zig"
```zig title="min_path_sum.zig" ```zig title="min_path_sum.zig"
@ -1111,6 +1123,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="min_path_sum.rb"
[class]{}-[func]{min_path_sum_dp}
```
=== "Zig" === "Zig"
```zig title="min_path_sum.zig" ```zig title="min_path_sum.zig"
@ -1505,6 +1523,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="min_path_sum.rb"
[class]{}-[func]{min_path_sum_dp_comp}
```
=== "Zig" === "Zig"
```zig title="min_path_sum.zig" ```zig title="min_path_sum.zig"

View file

@ -455,6 +455,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="edit_distance.rb"
[class]{}-[func]{edit_distance_dp}
```
=== "Zig" === "Zig"
```zig title="edit_distance.zig" ```zig title="edit_distance.zig"
@ -944,6 +950,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="edit_distance.rb"
[class]{}-[func]{edit_distance_dp_comp}
```
=== "Zig" === "Zig"
```zig title="edit_distance.zig" ```zig title="edit_distance.zig"

View file

@ -389,6 +389,14 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="climbing_stairs_backtrack.rb"
[class]{}-[func]{backtrack}
[class]{}-[func]{climbing_stairs_backtrack}
```
=== "Zig" === "Zig"
```zig title="climbing_stairs_backtrack.zig" ```zig title="climbing_stairs_backtrack.zig"
@ -680,6 +688,14 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="climbing_stairs_dfs.rb"
[class]{}-[func]{dfs}
[class]{}-[func]{climbing_stairs_dfs}
```
=== "Zig" === "Zig"
```zig title="climbing_stairs_dfs.zig" ```zig title="climbing_stairs_dfs.zig"
@ -1043,6 +1059,14 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="climbing_stairs_dfs_mem.rb"
[class]{}-[func]{dfs}
[class]{}-[func]{climbing_stairs_dfs_mem}
```
=== "Zig" === "Zig"
```zig title="climbing_stairs_dfs_mem.zig" ```zig title="climbing_stairs_dfs_mem.zig"
@ -1329,6 +1353,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="climbing_stairs_dp.rb"
[class]{}-[func]{climbing_stairs_dp}
```
=== "Zig" === "Zig"
```zig title="climbing_stairs_dp.zig" ```zig title="climbing_stairs_dp.zig"
@ -1574,6 +1604,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="climbing_stairs_dp.rb"
[class]{}-[func]{climbing_stairs_dp_comp}
```
=== "Zig" === "Zig"
```zig title="climbing_stairs_dp.zig" ```zig title="climbing_stairs_dp.zig"

View file

@ -321,6 +321,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="knapsack.rb"
[class]{}-[func]{knapsack_dfs}
```
=== "Zig" === "Zig"
```zig title="knapsack.zig" ```zig title="knapsack.zig"
@ -691,6 +697,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="knapsack.rb"
[class]{}-[func]{knapsack_dfs_mem}
```
=== "Zig" === "Zig"
```zig title="knapsack.zig" ```zig title="knapsack.zig"
@ -1049,6 +1061,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="knapsack.rb"
[class]{}-[func]{knapsack_dp}
```
=== "Zig" === "Zig"
```zig title="knapsack.zig" ```zig title="knapsack.zig"
@ -1435,6 +1453,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="knapsack.rb"
[class]{}-[func]{knapsack_dp_comp}
```
=== "Zig" === "Zig"
```zig title="knapsack.zig" ```zig title="knapsack.zig"

View file

@ -352,6 +352,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="unbounded_knapsack.rb"
[class]{}-[func]{unbounded_knapsack_dp}
```
=== "Zig" === "Zig"
```zig title="unbounded_knapsack.zig" ```zig title="unbounded_knapsack.zig"
@ -706,6 +712,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="unbounded_knapsack.rb"
[class]{}-[func]{unbounded_knapsack_dp_comp}
```
=== "Zig" === "Zig"
```zig title="unbounded_knapsack.zig" ```zig title="unbounded_knapsack.zig"
@ -1151,6 +1163,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="coin_change.rb"
[class]{}-[func]{coin_change_dp}
```
=== "Zig" === "Zig"
```zig title="coin_change.zig" ```zig title="coin_change.zig"
@ -1568,6 +1586,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="coin_change.rb"
[class]{}-[func]{coin_change_dp_comp}
```
=== "Zig" === "Zig"
```zig title="coin_change.zig" ```zig title="coin_change.zig"
@ -1975,6 +1999,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="coin_change_ii.rb"
[class]{}-[func]{coin_change_ii_dp}
```
=== "Zig" === "Zig"
```zig title="coin_change_ii.zig" ```zig title="coin_change_ii.zig"
@ -2313,6 +2343,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="coin_change_ii.rb"
[class]{}-[func]{coin_change_ii_dp_comp}
```
=== "Zig" === "Zig"
```zig title="coin_change_ii.zig" ```zig title="coin_change_ii.zig"

View file

@ -1124,6 +1124,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="graph_adjacency_matrix.rb"
[class]{GraphAdjMat}-[func]{}
```
=== "Zig" === "Zig"
```zig title="graph_adjacency_matrix.zig" ```zig title="graph_adjacency_matrix.zig"
@ -2221,6 +2227,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="graph_adjacency_list.rb"
[class]{GraphAdjList}-[func]{}
```
=== "Zig" === "Zig"
```zig title="graph_adjacency_list.zig" ```zig title="graph_adjacency_list.zig"

View file

@ -444,6 +444,12 @@ BFS 通常借助队列来实现,代码如下所示。队列具有“先入先
} }
``` ```
=== "Ruby"
```ruby title="graph_bfs.rb"
[class]{}-[func]{graph_bfs}
```
=== "Zig" === "Zig"
```zig title="graph_bfs.zig" ```zig title="graph_bfs.zig"
@ -885,6 +891,14 @@ BFS 通常借助队列来实现,代码如下所示。队列具有“先入先
} }
``` ```
=== "Ruby"
```ruby title="graph_dfs.rb"
[class]{}-[func]{dfs}
[class]{}-[func]{graph_dfs}
```
=== "Zig" === "Zig"
```zig title="graph_dfs.zig" ```zig title="graph_dfs.zig"

View file

@ -528,6 +528,14 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="fractional_knapsack.rb"
[class]{Item}-[func]{}
[class]{}-[func]{fractional_knapsack}
```
=== "Zig" === "Zig"
```zig title="fractional_knapsack.zig" ```zig title="fractional_knapsack.zig"

View file

@ -307,6 +307,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="coin_change_greedy.rb"
[class]{}-[func]{coin_change_greedy}
```
=== "Zig" === "Zig"
```zig title="coin_change_greedy.zig" ```zig title="coin_change_greedy.zig"

View file

@ -394,6 +394,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="max_capacity.rb"
[class]{}-[func]{max_capacity}
```
=== "Zig" === "Zig"
```zig title="max_capacity.zig" ```zig title="max_capacity.zig"

View file

@ -368,6 +368,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="max_product_cutting.rb"
[class]{}-[func]{max_product_cutting}
```
=== "Zig" === "Zig"
```zig title="max_product_cutting.zig" ```zig title="max_product_cutting.zig"

View file

@ -594,6 +594,18 @@ index = hash(key) % capacity
} }
``` ```
=== "Ruby"
```ruby title="simple_hash.rb"
[class]{}-[func]{add_hash}
[class]{}-[func]{mul_hash}
[class]{}-[func]{xor_hash}
[class]{}-[func]{rot_hash}
```
=== "Zig" === "Zig"
```zig title="simple_hash.zig" ```zig title="simple_hash.zig"
@ -911,6 +923,34 @@ $$
=== "Kotlin" === "Kotlin"
```kotlin title="built_in_hash.kt" ```kotlin title="built_in_hash.kt"
val num = 3
val hashNum = num.hashCode()
// 整数 3 的哈希值为 3
val bol = true
val hashBol = bol.hashCode()
// 布尔量 true 的哈希值为 1231
val dec = 3.14159
val hashDec = dec.hashCode()
// 小数 3.14159 的哈希值为 -1340954729
val str = "Hello 算法"
val hashStr = str.hashCode()
// 字符串“Hello 算法”的哈希值为 -727081396
val arr = arrayOf<Any>(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"
``` ```

View file

@ -1426,6 +1426,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="hash_map_chaining.rb"
[class]{HashMapChaining}-[func]{}
```
=== "Zig" === "Zig"
```zig title="hash_map_chaining.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"
```zig title="hash_map_open_addressing.zig" ```zig title="hash_map_open_addressing.zig"

View file

@ -280,6 +280,29 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="hash_map.kt" ```kotlin title="hash_map.kt"
/* 初始化哈希表 */
val map = HashMap<Int,String>()
/* 添加操作 */
// 在哈希表中添加键值对 (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"
```kotlin title="hash_map.kt" ```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"
```zig title="array_hash_map.zig" ```zig title="array_hash_map.zig"

View file

@ -305,6 +305,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="my_heap.rb"
[class]{MaxHeap}-[func]{__init__}
```
=== "Zig" === "Zig"
```zig title="my_heap.zig" ```zig title="my_heap.zig"

View file

@ -378,6 +378,43 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="heap.kt" ```kotlin title="heap.kt"
/* 初始化堆 */
// 初始化小顶堆
var minHeap = PriorityQueue<Int>()
// 初始化大顶堆(使用 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"
```zig title="my_heap.zig" ```zig title="my_heap.zig"
@ -767,6 +814,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="my_heap.rb"
[class]{MaxHeap}-[func]{peek}
```
=== "Zig" === "Zig"
```zig title="my_heap.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"
```zig title="my_heap.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"
```zig title="my_heap.zig" ```zig title="my_heap.zig"

View file

@ -434,6 +434,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="top_k.rb"
[class]{}-[func]{top_k_heap}
```
=== "Zig" === "Zig"
```zig title="top_k.zig" ```zig title="top_k.zig"

View file

@ -173,6 +173,12 @@ comments: true
*/ */
``` ```
=== "Ruby"
```ruby title=""
```
=== "Zig" === "Zig"
```zig title="" ```zig title=""

View file

@ -336,6 +336,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="binary_search.rb"
[class]{}-[func]{binary_search}
```
=== "Zig" === "Zig"
```zig title="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"
```zig title="binary_search.zig" ```zig title="binary_search.zig"

View file

@ -209,6 +209,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="binary_search_edge.rb"
[class]{}-[func]{binary_search_left_edge}
```
=== "Zig" === "Zig"
```zig title="binary_search_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"
```zig title="binary_search_edge.zig" ```zig title="binary_search_edge.zig"

View file

@ -290,6 +290,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="binary_search_insertion.rb"
[class]{}-[func]{binary_search_insertion_simple}
```
=== "Zig" === "Zig"
```zig title="binary_search_insertion.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"
```zig title="binary_search_insertion.zig" ```zig title="binary_search_insertion.zig"

View file

@ -225,6 +225,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="two_sum.rb"
[class]{}-[func]{two_sum_brute_force}
```
=== "Zig" === "Zig"
```zig title="two_sum.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"
```zig title="two_sum.zig" ```zig title="two_sum.zig"

View file

@ -271,6 +271,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="bubble_sort.rb"
[class]{}-[func]{bubble_sort}
```
=== "Zig" === "Zig"
```zig title="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"
```zig title="bubble_sort.zig" ```zig title="bubble_sort.zig"

View file

@ -345,8 +345,8 @@ comments: true
int k = size / 2; int k = size / 2;
float **buckets = calloc(k, sizeof(float *)); float **buckets = calloc(k, sizeof(float *));
for (int i = 0; i < k; i++) { for (int i = 0; i < k; i++) {
// 每个桶最多可以分配 k 个元素 // 每个桶最多可以分配 size 个元素
buckets[i] = calloc(ARRAY_SIZE, sizeof(float)); buckets[i] = calloc(size, sizeof(float));
} }
// 1. 将数组元素分配到各个桶中 // 1. 将数组元素分配到各个桶中
@ -359,7 +359,7 @@ comments: true
j++; j++;
} }
float temp = nums[i]; 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]); swap(&temp, &buckets[bucket_idx][j]);
j++; j++;
} }
@ -368,12 +368,12 @@ comments: true
// 2. 对各个桶执行排序 // 2. 对各个桶执行排序
for (int i = 0; i < k; i++) { 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. 遍历桶合并结果 // 3. 遍历桶合并结果
for (int i = 0, j = 0; j < k; j++) { 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) { if (buckets[j][l] > 0) {
nums[i++] = buckets[j][l]; nums[i++] = buckets[j][l];
} }
@ -421,6 +421,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="bucket_sort.rb"
[class]{}-[func]{bucket_sort}
```
=== "Zig" === "Zig"
```zig title="bucket_sort.zig" ```zig title="bucket_sort.zig"

View file

@ -345,6 +345,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="counting_sort.rb"
[class]{}-[func]{counting_sort_naive}
```
=== "Zig" === "Zig"
```zig title="counting_sort.zig" ```zig title="counting_sort.zig"
@ -845,6 +851,12 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="counting_sort.rb"
[class]{}-[func]{counting_sort}
```
=== "Zig" === "Zig"
```zig title="counting_sort.zig" ```zig title="counting_sort.zig"

View file

@ -572,6 +572,14 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="heap_sort.rb"
[class]{}-[func]{sift_down}
[class]{}-[func]{heap_sort}
```
=== "Zig" === "Zig"
```zig title="heap_sort.zig" ```zig title="heap_sort.zig"

View file

@ -250,6 +250,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="insertion_sort.rb"
[class]{}-[func]{insertion_sort}
```
=== "Zig" === "Zig"
```zig title="insertion_sort.zig" ```zig title="insertion_sort.zig"

View file

@ -625,6 +625,14 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="merge_sort.rb"
[class]{}-[func]{merge}
[class]{}-[func]{merge_sort}
```
=== "Zig" === "Zig"
```zig title="merge_sort.zig" ```zig title="merge_sort.zig"

View file

@ -351,6 +351,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="quick_sort.rb"
[class]{QuickSort}-[func]{partition}
```
=== "Zig" === "Zig"
```zig title="quick_sort.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"
```zig title="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"
```zig title="quick_sort.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"
```zig title="quick_sort.zig" ```zig title="quick_sort.zig"

View file

@ -672,6 +672,16 @@ $$
} }
``` ```
=== "Ruby"
```ruby title="radix_sort.rb"
[class]{}-[func]{digit}
[class]{}-[func]{counting_sort_digit}
[class]{}-[func]{radix_sort}
```
=== "Zig" === "Zig"
```zig title="radix_sort.zig" ```zig title="radix_sort.zig"

View file

@ -300,6 +300,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="selection_sort.rb"
[class]{}-[func]{selection_sort}
```
=== "Zig" === "Zig"
```zig title="selection_sort.zig" ```zig title="selection_sort.zig"

View file

@ -293,7 +293,7 @@ comments: true
int size = deque.length; int size = deque.length;
/* 判断双向队列是否为空 */ /* 判断双向队列是否为空 */
bool isEmpty = deque.isEmpty;W bool isEmpty = deque.isEmpty;
``` ```
=== "Rust" === "Rust"
@ -337,6 +337,34 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="deque.kt" ```kotlin title="deque.kt"
/* 初始化双向队列 */
val deque = LinkedList<Int>()
/* 元素入队 */
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"
```zig title="linkedlist_deque.zig" ```zig title="linkedlist_deque.zig"
@ -2635,7 +2671,7 @@ comments: true
} }
// 计算队尾指针,指向队尾索引 + 1 // 计算队尾指针,指向队尾索引 + 1
rear := q.index(q.front + q.queSize) rear := q.index(q.front + q.queSize)
// 将 num 添加至队 // 将 num 添加至队
q.nums[rear] = num q.nums[rear] = num
q.queSize++ q.queSize++
} }
@ -3451,6 +3487,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="array_deque.rb"
[class]{ArrayDeque}-[func]{}
```
=== "Zig" === "Zig"
```zig title="array_deque.zig" ```zig title="array_deque.zig"

View file

@ -315,6 +315,32 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="queue.kt" ```kotlin title="queue.kt"
/* 初始化队列 */
val queue = LinkedList<Int>()
/* 元素入队 */
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"
```zig title="linkedlist_queue.zig" ```zig title="linkedlist_queue.zig"
@ -2176,6 +2208,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="array_queue.rb"
[class]{ArrayQueue}-[func]{}
```
=== "Zig" === "Zig"
```zig title="array_queue.zig" ```zig title="array_queue.zig"

View file

@ -309,6 +309,32 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="stack.kt" ```kotlin title="stack.kt"
/* 初始化栈 */
val stack = Stack<Int>()
/* 元素入栈 */
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"
```zig title="linkedlist_stack.zig" ```zig title="linkedlist_stack.zig"
@ -1621,10 +1653,7 @@ comments: true
/* 出栈 */ /* 出栈 */
fn pop(&mut self) -> Option<T> { fn pop(&mut self) -> Option<T> {
match self.stack.pop() { self.stack.pop()
Some(num) => Some(num),
None => None,
}
} }
/* 访问栈顶元素 */ /* 访问栈顶元素 */
@ -1745,6 +1774,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="array_stack.rb"
[class]{ArrayStack}-[func]{}
```
=== "Zig" === "Zig"
```zig title="array_stack.zig" ```zig title="array_stack.zig"

View file

@ -123,6 +123,14 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="" ```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"
```zig title="array_binary_tree.zig" ```zig title="array_binary_tree.zig"

View file

@ -211,6 +211,17 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
=== "Kotlin" === "Kotlin"
```kotlin title="" ```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"
```zig title="avl_tree.zig" ```zig title="avl_tree.zig"
@ -616,6 +635,12 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
} }
``` ```
=== "Ruby"
```ruby title="avl_tree.rb"
[class]{AVLTree}-[func]{balance_factor}
```
=== "Zig" === "Zig"
```zig title="avl_tree.zig" ```zig title="avl_tree.zig"
@ -885,6 +910,12 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
} }
``` ```
=== "Ruby"
```ruby title="avl_tree.rb"
[class]{AVLTree}-[func]{right_rotate}
```
=== "Zig" === "Zig"
```zig title="avl_tree.zig" ```zig title="avl_tree.zig"
@ -1140,6 +1171,12 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
} }
``` ```
=== "Ruby"
```ruby title="avl_tree.rb"
[class]{AVLTree}-[func]{left_rotate}
```
=== "Zig" === "Zig"
```zig title="avl_tree.zig" ```zig title="avl_tree.zig"
@ -1608,6 +1645,12 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
} }
``` ```
=== "Ruby"
```ruby title="avl_tree.rb"
[class]{AVLTree}-[func]{rotate}
```
=== "Zig" === "Zig"
```zig title="avl_tree.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"
```zig title="avl_tree.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"
```zig title="avl_tree.zig" ```zig title="avl_tree.zig"

View file

@ -310,6 +310,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="binary_search_tree.rb"
[class]{BinarySearchTree}-[func]{search}
```
=== "Zig" === "Zig"
```zig title="binary_search_tree.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"
```zig title="binary_search_tree.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"
```zig title="binary_search_tree.zig" ```zig title="binary_search_tree.zig"

View file

@ -183,6 +183,16 @@ comments: true
=== "Kotlin" === "Kotlin"
```kotlin title="" ```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"
```kotlin title="binary_tree.kt" ```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"
```kotlin title="binary_tree.kt" ```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"
``` ```

View file

@ -314,6 +314,12 @@ comments: true
} }
``` ```
=== "Ruby"
```ruby title="binary_tree_bfs.rb"
[class]{}-[func]{level_order}
```
=== "Zig" === "Zig"
```zig title="binary_tree_bfs.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"
```zig title="binary_tree_dfs.zig" ```zig title="binary_tree_dfs.zig"

View file

@ -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"
```zig title="array.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"
```zig title="array.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"
```zig title="array.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"
```zig title="array.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"
```zig title="array.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"
```zig title="array.zig" ```zig title="array.zig"

View file

@ -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"
```zig title="linked_list.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"
```zig title="linked_list.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"
```zig title="linked_list.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"
```zig title="linked_list.zig" ```zig title="linked_list.zig"

View file

@ -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"
```zig title="my_list.zig" ```zig title="my_list.zig"

View file

@ -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"
```zig title="iteration.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"
```zig title="iteration.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"
```zig title="iteration.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"
```zig title="iteration.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"
```zig title="recursion.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"
```kotlin title="recursion.kt" ```kotlin title="recursion.kt"
/* Kotlin tailrec 关键词使函数实现尾递归优化 */ /* 尾递归 */
tailrec fun tailRecur(n: Int, res: Int): Int { tailrec fun tailRecur(n: Int, res: Int): Int {
// 添加 tailrec 关键词,以开启尾递归优化
// 终止条件 // 终止条件
if (n == 0) if (n == 0)
return res 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"
```zig title="recursion.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"
```zig title="recursion.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"
```zig title="recursion.zig" ```zig title="recursion.zig"

View file

@ -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"
```zig title="space_complexity.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"
```zig title="space_complexity.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"
```zig title="space_complexity.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"
```zig title="space_complexity.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"
```zig title="space_complexity.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"
```zig title="space_complexity.zig" ```zig title="space_complexity.zig"

View file

@ -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"
```zig title="time_complexity.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"
```zig title="time_complexity.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"
```zig title="time_complexity.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"
```zig title="time_complexity.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"
```zig title="time_complexity.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"
```zig title="time_complexity.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"
```zig title="time_complexity.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"
```zig title="time_complexity.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"
```zig title="time_complexity.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"
```zig title="time_complexity.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"
```zig title="time_complexity.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"
```zig title="worst_best_time_complexity.zig" ```zig title="worst_best_time_complexity.zig"

View file

@ -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"
```zig title="simple_hash.zig" ```zig title="simple_hash.zig"

View file

@ -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"
```zig title="hash_map_chaining.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"
```zig title="hash_map_open_addressing.zig" ```zig title="hash_map_open_addressing.zig"

View file

@ -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"
```zig title="array_hash_map.zig" ```zig title="array_hash_map.zig"

View file

@ -1968,6 +1968,14 @@ The implementation code is as follows:
} }
``` ```
=== "Ruby"
```ruby title="linkedlist_deque.rb"
[class]{ListNode}-[func]{}
[class]{LinkedListDeque}-[func]{}
```
=== "Zig" === "Zig"
```zig title="linkedlist_deque.zig" ```zig title="linkedlist_deque.zig"
@ -2634,7 +2642,7 @@ The implementation only needs to add methods for "front enqueue" and "rear deque
} }
// 计算队尾指针,指向队尾索引 + 1 // 计算队尾指针,指向队尾索引 + 1
rear := q.index(q.front + q.queSize) rear := q.index(q.front + q.queSize)
// 将 num 添加至队 // 将 num 添加至队
q.nums[rear] = num q.nums[rear] = num
q.queSize++ 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"
```zig title="array_deque.zig" ```zig title="array_deque.zig"

View file

@ -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"
```zig title="linkedlist_queue.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"
```zig title="array_queue.zig" ```zig title="array_queue.zig"

View file

@ -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"
```zig title="linkedlist_stack.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<T> { fn pop(&mut self) -> Option<T> {
match self.stack.pop() { self.stack.pop()
Some(num) => Some(num),
None => None,
}
} }
/* 访问栈顶元素 */ /* 访问栈顶元素 */
@ -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"
```zig title="array_stack.zig" ```zig title="array_stack.zig"

View file

@ -209,7 +209,7 @@ body {
/* code block tabs */ /* code block tabs */
.md-typeset .tabbed-labels>label { .md-typeset .tabbed-labels>label {
font-size: 0.59rem; font-size: 0.545rem;
} }
/* header banner */ /* header banner */