mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-18 02:50:19 +08:00
build
This commit is contained in:
@ -299,7 +299,7 @@ Accessing elements in an array is highly efficient, allowing us to randomly acce
|
||||
### 随机访问元素 ###
|
||||
def random_access(nums)
|
||||
# 在区间 [0, nums.length) 中随机抽取一个数字
|
||||
random_index = Random.rand 0...(nums.length - 1)
|
||||
random_index = Random.rand(0...nums.length)
|
||||
|
||||
# 获取并返回随机元素
|
||||
nums[random_index]
|
||||
|
@ -2154,7 +2154,7 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
@capacity = 10
|
||||
@size = 0
|
||||
@extend_ratio = 2
|
||||
@arr = Array.new capacity
|
||||
@arr = Array.new(capacity)
|
||||
end
|
||||
|
||||
### 访问元素 ###
|
||||
@ -2226,9 +2226,9 @@ To enhance our understanding of how lists work, we will attempt to implement a s
|
||||
def to_array
|
||||
sz = size
|
||||
# 仅转换有效长度范围内的列表元素
|
||||
arr = Array.new sz
|
||||
arr = Array.new(sz)
|
||||
for i in 0...sz
|
||||
arr[i] = get i
|
||||
arr[i] = get(i)
|
||||
end
|
||||
arr
|
||||
end
|
||||
|
@ -185,7 +185,17 @@ The following function uses a `for` loop to perform a summation of $1 + 2 + \dot
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="iteration.rb"
|
||||
[class]{}-[func]{for_loop}
|
||||
### for 循环 ###
|
||||
def for_loop(n)
|
||||
res = 0
|
||||
|
||||
# 循环求和 1, 2, ..., n-1, n
|
||||
for i in 1..n
|
||||
res += i
|
||||
end
|
||||
|
||||
res
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -417,7 +427,19 @@ Below we use a `while` loop to implement the sum $1 + 2 + \dots + n$.
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="iteration.rb"
|
||||
[class]{}-[func]{while_loop}
|
||||
### while 循环 ###
|
||||
def while_loop(n)
|
||||
res = 0
|
||||
i = 1 # 初始化条件变量
|
||||
|
||||
# 循环求和 1, 2, ..., n-1, n
|
||||
while i <= n
|
||||
res += i
|
||||
i += 1 # 更新条件变量
|
||||
end
|
||||
|
||||
res
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -664,7 +686,21 @@ For example, in the following code, the condition variable $i$ is updated twice
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="iteration.rb"
|
||||
[class]{}-[func]{while_loop_ii}
|
||||
### while 循环(两次更新)###
|
||||
def while_loop_ii(n)
|
||||
res = 0
|
||||
i = 1 # 初始化条件变量
|
||||
|
||||
# 循环求和 1, 4, 10, ...
|
||||
while i <= n
|
||||
res += i
|
||||
# 更新条件变量
|
||||
i += 1
|
||||
i *= 2
|
||||
end
|
||||
|
||||
res
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -904,7 +940,20 @@ 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}
|
||||
### 双层 for 循环 ###
|
||||
def nested_for_loop(n)
|
||||
res = ""
|
||||
|
||||
# 循环 i = 1, 2, ..., n-1, n
|
||||
for i in 1..n
|
||||
# 循环 j = 1, 2, ..., n-1, n
|
||||
for j in 1..n
|
||||
res += "(#{i}, #{j}), "
|
||||
end
|
||||
end
|
||||
|
||||
res
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -1139,7 +1188,15 @@ Observe the following code, where simply calling the function `recur(n)` can com
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="recursion.rb"
|
||||
[class]{}-[func]{recur}
|
||||
### 递归 ###
|
||||
def recur(n)
|
||||
# 终止条件
|
||||
return 1 if n == 1
|
||||
# 递:递归调用
|
||||
res = recur(n - 1)
|
||||
# 归:返回结果
|
||||
n + res
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -1362,7 +1419,13 @@ For example, in calculating $1 + 2 + \dots + n$, we can make the result variable
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="recursion.rb"
|
||||
[class]{}-[func]{tail_recur}
|
||||
### 尾递归 ###
|
||||
def tail_recur(n, res)
|
||||
# 终止条件
|
||||
return res if n == 0
|
||||
# 尾递归调用
|
||||
tail_recur(n - 1, res + n)
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -1594,7 +1657,15 @@ Using the recursive relation, and considering the first two numbers as terminati
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="recursion.rb"
|
||||
[class]{}-[func]{fib}
|
||||
### 斐波那契数列:递归 ###
|
||||
def fib(n)
|
||||
# 终止条件 f(1) = 0, f(2) = 1
|
||||
return n - 1 if n == 1 || n == 2
|
||||
# 递归调用 f(n) = f(n-1) + f(n-2)
|
||||
res = fib(n - 1) + fib(n - 2)
|
||||
# 返回结果 f(n)
|
||||
res
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -1936,7 +2007,25 @@ 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}
|
||||
### 使用迭代模拟递归 ###
|
||||
def for_loop_recur(n)
|
||||
# 使用一个显式的栈来模拟系统调用栈
|
||||
stack = []
|
||||
res = 0
|
||||
|
||||
# 递:递归调用
|
||||
for i in n.downto(0)
|
||||
# 通过“入栈操作”模拟“递”
|
||||
stack << i
|
||||
end
|
||||
# 归:返回结果
|
||||
while !stack.empty?
|
||||
res += stack.pop
|
||||
end
|
||||
|
||||
# res = 1+2+3+...+n
|
||||
res
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
@ -1080,9 +1080,24 @@ Note that memory occupied by initializing variables or calling functions in a lo
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="space_complexity.rb"
|
||||
[class]{}-[func]{function}
|
||||
### 函数 ###
|
||||
def function
|
||||
# 执行某些操作
|
||||
0
|
||||
end
|
||||
|
||||
[class]{}-[func]{constant}
|
||||
### 常数阶 ###
|
||||
def constant(n)
|
||||
# 常量、变量、对象占用 O(1) 空间
|
||||
a = 0
|
||||
nums = [0] * 10000
|
||||
node = ListNode.new
|
||||
|
||||
# 循环中的变量占用 O(1) 空间
|
||||
(0...n).each { c = 0 }
|
||||
# 循环中的函数占用 O(1) 空间
|
||||
(0...n).each { function }
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -1384,7 +1399,17 @@ Linear order is common in arrays, linked lists, stacks, queues, etc., where the
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="space_complexity.rb"
|
||||
[class]{}-[func]{linear}
|
||||
### 线性阶 ###
|
||||
def linear(n)
|
||||
# 长度为 n 的列表占用 O(n) 空间
|
||||
nums = Array.new(n, 0)
|
||||
|
||||
# 长度为 n 的哈希表占用 O(n) 空间
|
||||
hmap = {}
|
||||
for i in 0...n
|
||||
hmap[i] = i.to_s
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -1566,7 +1591,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}
|
||||
### 线性阶(递归实现)###
|
||||
def linear_recur(n)
|
||||
puts "递归 n = #{n}"
|
||||
return if n == 1
|
||||
linear_recur(n - 1)
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -1806,7 +1836,11 @@ Quadratic order is common in matrices and graphs, where the number of elements i
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="space_complexity.rb"
|
||||
[class]{}-[func]{quadratic}
|
||||
### 平方阶 ###
|
||||
def quadratic(n)
|
||||
# 二维列表占用 O(n^2) 空间
|
||||
Array.new(n) { Array.new(n, 0) }
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -2001,7 +2035,14 @@ 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}
|
||||
### 平方阶(递归实现)###
|
||||
def quadratic_recur(n)
|
||||
return 0 unless n > 0
|
||||
|
||||
# 数组 nums 长度为 n, n-1, ..., 2, 1
|
||||
nums = Array.new(n, 0)
|
||||
quadratic_recur(n - 1)
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -2199,7 +2240,15 @@ Exponential order is common in binary trees. Observe the below image, a "full bi
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="space_complexity.rb"
|
||||
[class]{}-[func]{build_tree}
|
||||
### 指数阶(建立满二叉树)###
|
||||
def build_tree(n)
|
||||
return if n == 0
|
||||
|
||||
TreeNode.new.tap do |root|
|
||||
root.left = build_tree(n - 1)
|
||||
root.right = build_tree(n - 1)
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
@ -1143,7 +1143,15 @@ Constant order means the number of operations is independent of the input data s
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="time_complexity.rb"
|
||||
[class]{}-[func]{constant}
|
||||
### 常数阶 ###
|
||||
def constant(n)
|
||||
count = 0
|
||||
size = 100000
|
||||
|
||||
(0...size).each { count += 1 }
|
||||
|
||||
count
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -1321,7 +1329,12 @@ Linear order indicates the number of operations grows linearly with the input da
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="time_complexity.rb"
|
||||
[class]{}-[func]{linear}
|
||||
### 线性阶 ###
|
||||
def linear(n)
|
||||
count = 0
|
||||
(0...n).each { count += 1 }
|
||||
count
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -1514,7 +1527,17 @@ Operations like array traversal and linked list traversal have a time complexity
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="time_complexity.rb"
|
||||
[class]{}-[func]{array_traversal}
|
||||
### 线性阶(遍历数组)###
|
||||
def array_traversal(nums)
|
||||
count = 0
|
||||
|
||||
# 循环次数与数组长度成正比
|
||||
for num in nums
|
||||
count += 1
|
||||
end
|
||||
|
||||
count
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -1734,7 +1757,19 @@ Quadratic order means the number of operations grows quadratically with the inpu
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="time_complexity.rb"
|
||||
[class]{}-[func]{quadratic}
|
||||
### 平方阶 ###
|
||||
def quadratic(n)
|
||||
count = 0
|
||||
|
||||
# 循环次数与数据大小 n 成平方关系
|
||||
for i in 0...n
|
||||
for j in 0...n
|
||||
count += 1
|
||||
end
|
||||
end
|
||||
|
||||
count
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -2040,7 +2075,26 @@ 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}
|
||||
### 平方阶(冒泡排序)###
|
||||
def bubble_sort(nums)
|
||||
count = 0 # 计数器
|
||||
|
||||
# 外循环:未排序区间为 [0, i]
|
||||
for i in (nums.length - 1).downto(0)
|
||||
# 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
for j in 0...i
|
||||
if nums[j] > nums[j + 1]
|
||||
# 交换 nums[j] 与 nums[j + 1]
|
||||
tmp = nums[j]
|
||||
nums[j] = nums[j + 1]
|
||||
nums[j + 1] = tmp
|
||||
count += 3 # 元素交换包含 3 个单元操作
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
count
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -2302,7 +2356,19 @@ The following image and code simulate the cell division process, with a time com
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="time_complexity.rb"
|
||||
[class]{}-[func]{exponential}
|
||||
### 指数阶(循环实现)###
|
||||
def exponential(n)
|
||||
count, base = 0, 1
|
||||
|
||||
# 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
(0...n).each do
|
||||
(0...base).each { count += 1 }
|
||||
base *= 2
|
||||
end
|
||||
|
||||
# count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
count
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -2471,7 +2537,11 @@ In practice, exponential order often appears in recursive functions. For example
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="time_complexity.rb"
|
||||
[class]{}-[func]{exp_recur}
|
||||
### 指数阶(递归实现)###
|
||||
def exp_recur(n)
|
||||
return 1 if n == 1
|
||||
exp_recur(n - 1) + exp_recur(n - 1) + 1
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -2668,7 +2738,17 @@ The following image and code simulate the "halving each round" process, with a t
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="time_complexity.rb"
|
||||
[class]{}-[func]{logarithmic}
|
||||
### 对数阶(循环实现)###
|
||||
def logarithmic(n)
|
||||
count = 0
|
||||
|
||||
while n > 1
|
||||
n /= 2
|
||||
count += 1
|
||||
end
|
||||
|
||||
count
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -2831,7 +2911,11 @@ Like exponential order, logarithmic order also frequently appears in recursive f
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="time_complexity.rb"
|
||||
[class]{}-[func]{log_recur}
|
||||
### 对数阶(递归实现)###
|
||||
def log_recur(n)
|
||||
return 0 unless n > 1
|
||||
log_recur(n / 2) + 1
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -3045,7 +3129,15 @@ Linear-logarithmic order often appears in nested loops, with the complexities of
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="time_complexity.rb"
|
||||
[class]{}-[func]{linear_log_recur}
|
||||
### 线性对数阶 ###
|
||||
def linear_log_recur(n)
|
||||
return 1 unless n > 1
|
||||
|
||||
count = linear_log_recur(n / 2) + linear_log_recur(n / 2)
|
||||
(0...n).each { count += 1 }
|
||||
|
||||
count
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -3277,7 +3369,16 @@ Factorials are typically implemented using recursion. As shown in the image and
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="time_complexity.rb"
|
||||
[class]{}-[func]{factorial_recur}
|
||||
### 阶乘阶(递归实现)###
|
||||
def factorial_recur(n)
|
||||
return 1 if n == 0
|
||||
|
||||
count = 0
|
||||
# 从 1 个分裂出 n 个
|
||||
(0...n).each { count += factorial_recur(n - 1) }
|
||||
|
||||
count
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -3672,9 +3773,24 @@ The "worst-case time complexity" corresponds to the asymptotic upper bound, deno
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="worst_best_time_complexity.rb"
|
||||
[class]{}-[func]{random_numbers}
|
||||
### 生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱 ###
|
||||
def random_numbers(n)
|
||||
# 生成数组 nums =: 1, 2, 3, ..., n
|
||||
nums = Array.new(n) { |i| i + 1 }
|
||||
# 随机打乱数组元素
|
||||
nums.shuffle!
|
||||
end
|
||||
|
||||
[class]{}-[func]{find_one}
|
||||
### 查找数组 nums 中数字 1 所在索引 ###
|
||||
def find_one(nums)
|
||||
for i in 0...nums.length
|
||||
# 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
# 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
return i if nums[i] == 1
|
||||
end
|
||||
|
||||
-1
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
Reference in New Issue
Block a user