mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
完善所以c#相关的文档和代码
This commit is contained in:
@ -149,7 +149,29 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
/* 类 */
|
||||
class Node
|
||||
{
|
||||
int val;
|
||||
Node next;
|
||||
Node(int x) { val = x; }
|
||||
}
|
||||
|
||||
/* 函数(或称方法) */
|
||||
int function()
|
||||
{
|
||||
// do something...
|
||||
return 0;
|
||||
}
|
||||
|
||||
int algorithm(int n)
|
||||
{ // 输入数据
|
||||
int a = 0; // 暂存数据(常量)
|
||||
int b = 0; // 暂存数据(变量)
|
||||
Node node = new Node(0); // 暂存数据(对象)
|
||||
int c = function(); // 栈帧空间(调用函数)
|
||||
return a + b + c; // 输出数据
|
||||
}
|
||||
```
|
||||
|
||||
## 推算方法
|
||||
@ -228,7 +250,15 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
|
||||
void algorithm(int n)
|
||||
{
|
||||
int a = 0; // O(1)
|
||||
int[] b = new int[10000]; // O(1)
|
||||
if (n > 10)
|
||||
{
|
||||
int[] nums = new int[n]; // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**在递归函数中,需要注意统计栈帧空间。** 例如函数 `loop()`,在循环中调用了 $n$ 次 `function()` ,每轮中的 `function()` 都返回并释放了栈帧空间,因此空间复杂度仍为 $O(1)$ 。而递归函数 `recur()` 在运行中会同时存在 $n$ 个未返回的 `recur()` ,从而使用 $O(n)$ 的栈帧空间。
|
||||
@ -330,13 +360,31 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
|
||||
int function()
|
||||
{
|
||||
// do something
|
||||
return 0;
|
||||
}
|
||||
/* 循环 O(1) */
|
||||
void loop(int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
function();
|
||||
}
|
||||
}
|
||||
/* 递归 O(n) */
|
||||
int recur(int n)
|
||||
{
|
||||
if (n == 1) return 1;
|
||||
return recur(n - 1);
|
||||
}
|
||||
```
|
||||
|
||||
## 常见类型
|
||||
@ -467,7 +515,25 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity.cs"
|
||||
|
||||
/* 常数阶 */
|
||||
void constant(int n)
|
||||
{
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
int[] nums = new int[10000];
|
||||
ListNode node = new ListNode(0);
|
||||
// 循环中的变量占用 O(1) 空间
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
int c = 0;
|
||||
}
|
||||
// 循环中的函数占用 O(1) 空间
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
function();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 线性阶 $O(n)$
|
||||
@ -568,7 +634,24 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity.cs"
|
||||
|
||||
/* 线性阶 */
|
||||
void linear(int n)
|
||||
{
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
int[] nums = new int[n];
|
||||
// 长度为 n 的列表占用 O(n) 空间
|
||||
List<ListNode> nodes = new();
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
nodes.Add(new ListNode(i));
|
||||
}
|
||||
// 长度为 n 的哈希表占用 O(n) 空间
|
||||
Dictionary<int, String> map = new();
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
map.Add(i, i.ToString());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
以下递归函数会同时存在 $n$ 个未返回的 `algorithm()` 函数,使用 $O(n)$ 大小的栈帧空间。
|
||||
@ -639,7 +722,13 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity.cs"
|
||||
|
||||
/* 线性阶(递归实现) */
|
||||
void linearRecur(int n)
|
||||
{
|
||||
Console.WriteLine("递归 n = " + n);
|
||||
if (n == 1) return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@ -729,6 +818,23 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity.cs"
|
||||
/* 平方阶 */
|
||||
void quadratic(int n)
|
||||
{
|
||||
// 矩阵占用 O(n^2) 空间
|
||||
int[,] numMatrix = new int[n, n];
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
List<List<int>> numList = new();
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
List<int> tmp = new();
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
tmp.Add(0);
|
||||
}
|
||||
numList.Add(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@ -804,6 +910,14 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity.cs"
|
||||
/* 平方阶(递归实现) */
|
||||
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);
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@ -889,7 +1003,15 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity.cs"
|
||||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
TreeNode? buildTree(int n)
|
||||
{
|
||||
if (n == 0) return null;
|
||||
TreeNode root = new TreeNode(0);
|
||||
root.left = buildTree(n - 1);
|
||||
root.right = buildTree(n - 1);
|
||||
return root;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
|
||||
Reference in New Issue
Block a user