mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-09 14:06:06 +00: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;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
|
||||
@@ -130,7 +130,23 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title="leetcode_two_sum.cs"
|
||||
|
||||
class SolutionBruteForce
|
||||
{
|
||||
public int[] twoSum(int[] nums, int target)
|
||||
{
|
||||
int size = nums.Length;
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
for (int i = 0; i < size - 1; i++)
|
||||
{
|
||||
for (int j = i + 1; j < size; j++)
|
||||
{
|
||||
if (nums[i] + nums[j] == target)
|
||||
return new int[] { i, j };
|
||||
}
|
||||
}
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 方法二:辅助哈希表
|
||||
@@ -258,5 +274,23 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title="leetcode_two_sum.cs"
|
||||
|
||||
class SolutionHashMap
|
||||
{
|
||||
public int[] twoSum(int[] nums, int target)
|
||||
{
|
||||
int size = nums.Length;
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
Dictionary<int, int> dic = new();
|
||||
// 单层循环,时间复杂度 O(n)
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
if (dic.ContainsKey(target - nums[i]))
|
||||
{
|
||||
return new int[] { dic[target - nums[i]], i };
|
||||
}
|
||||
dic.Add(nums[i], i);
|
||||
}
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -97,7 +97,18 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
|
||||
// 在某运行平台下
|
||||
void algorithm(int n)
|
||||
{
|
||||
int a = 2; // 1 ns
|
||||
a = a + 1; // 1 ns
|
||||
a = a * 2; // 10 ns
|
||||
// 循环 n 次
|
||||
for (int i = 0; i < n; i++)
|
||||
{ // 1 ns ,每轮都要执行 i++
|
||||
Console.WriteLine(0); // 5 ns
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
但实际上, **统计算法的运行时间既不合理也不现实。** 首先,我们不希望预估时间和运行平台绑定,毕竟算法需要跑在各式各样的平台之上。其次,我们很难获知每一种操作的运行时间,这为预估过程带来了极大的难度。
|
||||
@@ -212,7 +223,27 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
|
||||
// 算法 A 时间复杂度:常数阶
|
||||
void algorithm_A(int n)
|
||||
{
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
// 算法 B 时间复杂度:线性阶
|
||||
void algorithm_B(int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
}
|
||||
// 算法 C 时间复杂度:常数阶
|
||||
void algorithm_C(int n)
|
||||
{
|
||||
for (int i = 0; i < 1000000; i++)
|
||||
{
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@@ -310,7 +341,15 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
|
||||
void algorithm(int n) {
|
||||
int a = 1; // +1
|
||||
a = a + 1; // +1
|
||||
a = a * 2; // +1
|
||||
// 循环 n 次
|
||||
for (int i = 0; i < n; i++) { // +1(每轮都执行 i ++)
|
||||
Console.WriteLine(0); // +1
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
$T(n)$ 是个一次函数,说明时间增长趋势是线性的,因此易得时间复杂度是线性阶。
|
||||
@@ -457,7 +496,24 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
|
||||
void algorithm(int n)
|
||||
{
|
||||
int a = 1; // +0(技巧 1)
|
||||
a = a + n; // +0(技巧 1)
|
||||
// +n(技巧 2)
|
||||
for (int i = 0; i < 5 * n + 1; i++)
|
||||
{
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
// +n*n(技巧 3)
|
||||
for (int i = 0; i < 2 * n; i++)
|
||||
{
|
||||
for (int j = 0; j < n + 1; j++)
|
||||
{
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. 判断渐近上界
|
||||
@@ -576,7 +632,15 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 常数阶 */
|
||||
int constant(int n)
|
||||
{
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
### 线性阶 $O(n)$
|
||||
@@ -652,7 +716,14 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 线性阶 */
|
||||
int linear(int n)
|
||||
{
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
「遍历数组」和「遍历链表」等操作,时间复杂度都为 $O(n)$ ,其中 $n$ 为数组或链表的长度。
|
||||
@@ -736,7 +807,17 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 线性阶(遍历数组) */
|
||||
int arrayTraversal(int[] nums)
|
||||
{
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
foreach(int num in nums)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
### 平方阶 $O(n^2)$
|
||||
@@ -825,7 +906,20 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 平方阶 */
|
||||
int quadratic(int n)
|
||||
{
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@@ -947,6 +1041,28 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 平方阶(冒泡排序) */
|
||||
int bubbleSort(int[] nums)
|
||||
{
|
||||
int count = 0; // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.Length - 1; i > 0; i--)
|
||||
{
|
||||
// 内循环:冒泡操作
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
if (nums[j] > nums[j + 1])
|
||||
{
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -1048,7 +1164,22 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 指数阶(循环实现) */
|
||||
int exponential(int n)
|
||||
{
|
||||
int count = 0, bas = 1;
|
||||
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < bas; j++)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
bas *= 2;
|
||||
}
|
||||
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@@ -1119,7 +1250,12 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n)
|
||||
{
|
||||
if (n == 1) return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
```
|
||||
|
||||
### 对数阶 $O(\log n)$
|
||||
@@ -1205,7 +1341,17 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 对数阶(循环实现) */
|
||||
int logarithmic(float n)
|
||||
{
|
||||
int count = 0;
|
||||
while (n > 1)
|
||||
{
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@@ -1276,7 +1422,12 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n)
|
||||
{
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
```
|
||||
|
||||
### 线性对数阶 $O(n \log n)$
|
||||
@@ -1366,7 +1517,18 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 线性对数阶 */
|
||||
int linearLogRecur(float n)
|
||||
{
|
||||
if (n <= 1) return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@@ -1464,7 +1626,18 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 阶乘阶(递归实现) */
|
||||
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);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@@ -1485,7 +1658,7 @@ $$
|
||||
```java title="worst_best_time_complexity.java"
|
||||
public class worst_best_time_complexity {
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
static int[] randomNumbers(int n) {
|
||||
int[] randomNumbers(int n) {
|
||||
Integer[] nums = new Integer[n];
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -1502,7 +1675,7 @@ $$
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
static int findOne(int[] nums) {
|
||||
int findOne(int[] nums) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
@@ -1511,7 +1684,7 @@ $$
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
public void main(String[] args) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int n = 100;
|
||||
int[] nums = randomNumbers(n);
|
||||
@@ -1652,7 +1825,51 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="worst_best_time_complexity.cs"
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
int[] randomNumbers(int n)
|
||||
{
|
||||
int[] nums = new int[n];
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
|
||||
// 随机打乱数组元素
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
var index = new Random().Next(i, nums.Length);
|
||||
var tmp = nums[i];
|
||||
var ran = nums[index];
|
||||
nums[i] = ran;
|
||||
nums[index] = tmp;
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
int findOne(int[] nums)
|
||||
{
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public void main(String[] args)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
int n = 100;
|
||||
int[] nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
Console.WriteLine("\n数组 [ 1, 2, ..., n ] 被打乱后 = " + string.Join(",", nums));
|
||||
Console.WriteLine("数字 1 的索引为 " + index);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
!!! tip
|
||||
|
||||
Reference in New Issue
Block a user