This commit is contained in:
krahets
2023-10-08 01:43:28 +08:00
parent 3d2d669b43
commit baac2d11a7
52 changed files with 999 additions and 625 deletions
@@ -61,7 +61,7 @@ status: new
```csharp title="iteration.cs"
/* for 循环 */
int forLoop(int n) {
int ForLoop(int n) {
int res = 0;
// 循环求和 1, 2, ..., n-1, n
for (int i = 1; i <= n; i++) {
@@ -239,7 +239,7 @@ status: new
```csharp title="iteration.cs"
/* while 循环 */
int whileLoop(int n) {
int WhileLoop(int n) {
int res = 0;
int i = 1; // 初始化条件变量
// 循环求和 1, 2, ..., n-1, n
@@ -431,7 +431,7 @@ status: new
```csharp title="iteration.cs"
/* while 循环(两次更新) */
int whileLoopII(int n) {
int WhileLoopII(int n) {
int res = 0;
int i = 1; // 初始化条件变量
// 循环求和 1, 2, 4, 5...
@@ -636,8 +636,8 @@ status: new
```csharp title="iteration.cs"
/* 双层 for 循环 */
string nestedForLoop(int n) {
StringBuilder res = new StringBuilder();
string NestedForLoop(int n) {
StringBuilder res = new();
// 循环 i = 1, 2, ..., n-1, n
for (int i = 1; i <= n; i++) {
// 循环 j = 1, 2, ..., n-1, n
@@ -851,12 +851,12 @@ status: new
```csharp title="recursion.cs"
/* 递归 */
int recur(int n) {
int Recur(int n) {
// 终止条件
if (n == 1)
return 1;
// 递:递归调用
int res = recur(n - 1);
int res = Recur(n - 1);
// 归:返回结果
return n + res;
}
@@ -1055,12 +1055,12 @@ status: new
```csharp title="recursion.cs"
/* 尾递归 */
int tailRecur(int n, int res) {
int TailRecur(int n, int res) {
// 终止条件
if (n == 0)
return res;
// 尾递归调用
return tailRecur(n - 1, res + n);
return TailRecur(n - 1, res + n);
}
```
@@ -1237,12 +1237,12 @@ status: new
```csharp title="recursion.cs"
/* 斐波那契数列:递归 */
int fib(int n) {
int Fib(int n) {
// 终止条件 f(1) = 0, f(2) = 1
if (n == 1 || n == 2)
return n - 1;
// 递归调用 f(n) = f(n-1) + f(n-2)
int res = fib(n - 1) + fib(n - 2);
int res = Fib(n - 1) + Fib(n - 2);
// 返回结果 f(n)
return res;
}
@@ -1471,9 +1471,9 @@ status: new
```csharp title="recursion.cs"
/* 使用迭代模拟递归 */
int forLoopRecur(int n) {
int ForLoopRecur(int n) {
// 使用一个显式的栈来模拟系统调用栈
Stack<int> stack = new Stack<int>();
Stack<int> stack = new();
int res = 0;
// 递:递归调用
for (int i = n; i > 0; i--) {
@@ -1493,7 +1493,25 @@ status: new
=== "Go"
```go title="recursion.go"
[class]{}-[func]{forLoopRecur}
/* 使用迭代模拟递归 */
func forLoopRecur(n int) int {
// 使用一个显式的栈来模拟系统调用栈
stack := list.New()
res := 0
// 递:递归调用
for i := n; i > 0; i-- {
// 通过“入栈操作”模拟“递”
stack.PushBack(i)
}
// 归:返回结果
for stack.Len() != 0 {
// 通过“出栈操作”模拟“归”
res += stack.Back().Value.(int)
stack.Remove(stack.Back())
}
// res = 1+2+3+...+n
return res
}
```
=== "Swift"
@@ -111,16 +111,16 @@ comments: true
}
/* 函数 */
int function() {
int Function() {
// 执行某些操作...
return 0;
}
int algorithm(int n) { // 输入数据
int Algorithm(int n) { // 输入数据
const int a = 0; // 暂存数据(常量)
int b = 0; // 暂存数据(变量)
Node node = new Node(0); // 暂存数据(对象)
int c = function(); // 栈帧空间(调用函数)
Node node = new(0); // 暂存数据(对象)
int c = Function(); // 栈帧空间(调用函数)
return a + b + c; // 输出数据
}
```
@@ -366,7 +366,7 @@ comments: true
=== "C#"
```csharp title=""
void algorithm(int n) {
void Algorithm(int n) {
int a = 0; // O(1)
int[] b = new int[10000]; // O(1)
if (n > 10) {
@@ -532,20 +532,20 @@ comments: true
=== "C#"
```csharp title=""
int function() {
int Function() {
// 执行某些操作
return 0;
}
/* 循环 O(1) */
void loop(int n) {
void Loop(int n) {
for (int i = 0; i < n; i++) {
function();
Function();
}
}
/* 递归 O(n) */
int recur(int n) {
int Recur(int n) {
if (n == 1) return 1;
return recur(n - 1);
return Recur(n - 1);
}
```
@@ -807,25 +807,25 @@ $$
```csharp title="space_complexity.cs"
/* 函数 */
int function() {
int Function() {
// 执行某些操作
return 0;
}
/* 常数阶 */
void constant(int n) {
void Constant(int n) {
// 常量、变量、对象占用 O(1) 空间
int a = 0;
int b = 0;
int[] nums = new int[10000];
ListNode node = new ListNode(0);
ListNode node = new(0);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++) {
int c = 0;
}
// 循环中的函数占用 O(1) 空间
for (int i = 0; i < n; i++) {
function();
Function();
}
}
```
@@ -1115,7 +1115,7 @@ $$
```csharp title="space_complexity.cs"
/* 线性阶 */
void linear(int n) {
void Linear(int n) {
// 长度为 n 的数组占用 O(n) 空间
int[] nums = new int[n];
// 长度为 n 的列表占用 O(n) 空间
@@ -1361,10 +1361,10 @@ $$
```csharp title="space_complexity.cs"
/* 线性阶(递归实现) */
void linearRecur(int n) {
void LinearRecur(int n) {
Console.WriteLine("递归 n = " + n);
if (n == 1) return;
linearRecur(n - 1);
LinearRecur(n - 1);
}
```
@@ -1518,7 +1518,7 @@ $$
```csharp title="space_complexity.cs"
/* 平方阶 */
void quadratic(int n) {
void Quadratic(int n) {
// 矩阵占用 O(n^2) 空间
int[,] numMatrix = new int[n, n];
// 二维列表占用 O(n^2) 空间
@@ -1726,11 +1726,11 @@ $$
```csharp title="space_complexity.cs"
/* 平方阶(递归实现) */
int quadraticRecur(int n) {
int QuadraticRecur(int n) {
if (n <= 0) return 0;
int[] nums = new int[n];
Console.WriteLine("递归 n = " + n + " 中的 nums 长度 = " + nums.Length);
return quadraticRecur(n - 1);
return QuadraticRecur(n - 1);
}
```
@@ -1893,11 +1893,12 @@ $$
```csharp title="space_complexity.cs"
/* 指数阶(建立满二叉树) */
TreeNode? buildTree(int n) {
TreeNode? BuildTree(int n) {
if (n == 0) return null;
TreeNode root = new TreeNode(0);
root.left = buildTree(n - 1);
root.right = buildTree(n - 1);
TreeNode root = new(0) {
left = BuildTree(n - 1),
right = BuildTree(n - 1)
};
return root;
}
```
@@ -59,7 +59,7 @@ comments: true
```csharp title=""
// 在某运行平台下
void algorithm(int n) {
void Algorithm(int n) {
int a = 2; // 1 ns
a = a + 1; // 1 ns
a = a * 2; // 10 ns
@@ -257,17 +257,17 @@ $$
```csharp title=""
// 算法 A 的时间复杂度:常数阶
void algorithm_A(int n) {
void AlgorithmA(int n) {
Console.WriteLine(0);
}
// 算法 B 的时间复杂度:线性阶
void algorithm_B(int n) {
void AlgorithmB(int n) {
for (int i = 0; i < n; i++) {
Console.WriteLine(0);
}
}
// 算法 C 的时间复杂度:常数阶
void algorithm_C(int n) {
void AlgorithmC(int n) {
for (int i = 0; i < 1000000; i++) {
Console.WriteLine(0);
}
@@ -493,7 +493,7 @@ $$
=== "C#"
```csharp title=""
void algorithm(int n) {
void Algorithm(int n) {
int a = 1; // +1
a = a + 1; // +1
a = a * 2; // +1
@@ -703,7 +703,7 @@ $T(n)$ 是一次函数,说明其运行时间的增长趋势是线性的,因
=== "C#"
```csharp title=""
void algorithm(int n) {
void Algorithm(int n) {
int a = 1; // +0(技巧 1
a = a + n; // +0(技巧 1
// +n(技巧 2
@@ -953,7 +953,7 @@ $$
```csharp title="time_complexity.cs"
/* 常数阶 */
int constant(int n) {
int Constant(int n) {
int count = 0;
int size = 100000;
for (int i = 0; i < size; i++)
@@ -1117,7 +1117,7 @@ $$
```csharp title="time_complexity.cs"
/* 线性阶 */
int linear(int n) {
int Linear(int n) {
int count = 0;
for (int i = 0; i < n; i++)
count++;
@@ -1272,7 +1272,7 @@ $$
```csharp title="time_complexity.cs"
/* 线性阶(遍历数组) */
int arrayTraversal(int[] nums) {
int ArrayTraversal(int[] nums) {
int count = 0;
// 循环次数与数组长度成正比
foreach (int num in nums) {
@@ -1449,7 +1449,7 @@ $$
```csharp title="time_complexity.cs"
/* 平方阶 */
int quadratic(int n) {
int Quadratic(int n) {
int count = 0;
// 循环次数与数组长度成平方关系
for (int i = 0; i < n; i++) {
@@ -1668,7 +1668,7 @@ $$
```csharp title="time_complexity.cs"
/* 平方阶(冒泡排序) */
int bubbleSort(int[] nums) {
int BubbleSort(int[] nums) {
int count = 0; // 计数器
// 外循环:未排序区间为 [0, i]
for (int i = nums.Length - 1; i > 0; i--) {
@@ -1933,7 +1933,7 @@ $$
```csharp title="time_complexity.cs"
/* 指数阶(循环实现) */
int exponential(int n) {
int Exponential(int n) {
int count = 0, bas = 1;
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++) {
@@ -2141,9 +2141,9 @@ $$
```csharp title="time_complexity.cs"
/* 指数阶(递归实现) */
int expRecur(int n) {
int ExpRecur(int n) {
if (n == 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;
}
```
@@ -2286,7 +2286,7 @@ $$
```csharp title="time_complexity.cs"
/* 对数阶(循环实现) */
int logarithmic(float n) {
int Logarithmic(float n) {
int count = 0;
while (n > 1) {
n = n / 2;
@@ -2453,9 +2453,9 @@ $$
```csharp title="time_complexity.cs"
/* 对数阶(递归实现) */
int logRecur(float n) {
int LogRecur(float n) {
if (n <= 1) return 0;
return logRecur(n / 2) + 1;
return LogRecur(n / 2) + 1;
}
```
@@ -2610,10 +2610,10 @@ $$
```csharp title="time_complexity.cs"
/* 线性对数阶 */
int linearLogRecur(float n) {
int LinearLogRecur(float n) {
if (n <= 1) return 1;
int count = linearLogRecur(n / 2) +
linearLogRecur(n / 2);
int count = LinearLogRecur(n / 2) +
LinearLogRecur(n / 2);
for (int i = 0; i < n; i++) {
count++;
}
@@ -2812,12 +2812,12 @@ $$
```csharp title="time_complexity.cs"
/* 阶乘阶(递归实现) */
int factorialRecur(int n) {
int FactorialRecur(int n) {
if (n == 0) return 1;
int count = 0;
// 从 1 个分裂出 n 个
for (int i = 0; i < n; i++) {
count += factorialRecur(n - 1);
count += FactorialRecur(n - 1);
}
return count;
}
@@ -3051,7 +3051,7 @@ $$
```csharp title="worst_best_time_complexity.cs"
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
int[] randomNumbers(int n) {
int[] RandomNumbers(int n) {
int[] nums = new int[n];
// 生成数组 nums = { 1, 2, 3, ..., n }
for (int i = 0; i < n; i++) {
@@ -3070,7 +3070,7 @@ $$
}
/* 查找数组 nums 中数字 1 所在索引 */
int findOne(int[] nums) {
int FindOne(int[] nums) {
for (int i = 0; i < nums.Length; i++) {
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)