mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-11 23:16:07 +00:00
build
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user