mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
Add build scripts for C# and
unify the coding style.
This commit is contained in:
@@ -641,25 +641,7 @@ $$
|
||||
=== "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();
|
||||
}
|
||||
}
|
||||
[class]{space_complexity}-[func]{constant}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -759,24 +741,7 @@ $$
|
||||
=== "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());
|
||||
}
|
||||
}
|
||||
[class]{space_complexity}-[func]{linear}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -866,13 +831,7 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity.cs"
|
||||
/* 线性阶(递归实现) */
|
||||
void linearRecur(int n)
|
||||
{
|
||||
Console.WriteLine("递归 n = " + n);
|
||||
if (n == 1) return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
[class]{space_complexity}-[func]{linearRecur}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -952,23 +911,7 @@ $$
|
||||
=== "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);
|
||||
}
|
||||
}
|
||||
[class]{space_complexity}-[func]{quadratic}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -1053,14 +996,7 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="space_complexity.cs"
|
||||
/* 平方阶(递归实现) */
|
||||
int quadraticRecur(int n)
|
||||
{
|
||||
if (n <= 0) return 0;
|
||||
// 数组 nums 长度为 n, n-1, ..., 2, 1
|
||||
int[] nums = new int[n];
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
[class]{space_complexity}-[func]{quadraticRecur}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -1143,15 +1079,7 @@ $$
|
||||
=== "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;
|
||||
}
|
||||
[class]{space_complexity}-[func]{buildTree}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
@@ -86,23 +86,7 @@ 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];
|
||||
}
|
||||
}
|
||||
[class]{leetcode_two_sum}-[func]{twoSumBruteForce}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -195,25 +179,7 @@ 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];
|
||||
}
|
||||
}
|
||||
[class]{leetcode_two_sum}-[func]{twoSumHashTable}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
@@ -854,15 +854,7 @@ $$
|
||||
=== "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;
|
||||
}
|
||||
[class]{time_complexity}-[func]{constant}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -950,14 +942,7 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 线性阶 */
|
||||
int linear(int n)
|
||||
{
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
[class]{time_complexity}-[func]{linear}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -1047,17 +1032,7 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 线性阶(遍历数组) */
|
||||
int arrayTraversal(int[] nums)
|
||||
{
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
foreach(int num in nums)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
[class]{time_complexity}-[func]{arrayTraversal}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -1149,20 +1124,7 @@ $$
|
||||
=== "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;
|
||||
}
|
||||
[class]{time_complexity}-[func]{quadratic}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -1280,29 +1242,7 @@ $$
|
||||
=== "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;
|
||||
}
|
||||
|
||||
[class]{time_complexity}-[func]{bubbleSort}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -1414,22 +1354,7 @@ $$
|
||||
=== "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;
|
||||
}
|
||||
[class]{time_complexity}-[func]{exponential}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -1520,12 +1445,7 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n)
|
||||
{
|
||||
if (n == 1) return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
[class]{time_complexity}-[func]{expRecur}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -1613,17 +1533,7 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 对数阶(循环实现) */
|
||||
int logarithmic(float n)
|
||||
{
|
||||
int count = 0;
|
||||
while (n > 1)
|
||||
{
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
[class]{time_complexity}-[func]{logarithmic}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -1710,12 +1620,7 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n)
|
||||
{
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
[class]{time_complexity}-[func]{logRecur}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -1806,18 +1711,7 @@ $$
|
||||
=== "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;
|
||||
}
|
||||
[class]{time_complexity}-[func]{linearLogRecur}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -1921,18 +1815,7 @@ $$
|
||||
=== "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;
|
||||
}
|
||||
[class]{time_complexity}-[func]{factorialRecur}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -2146,40 +2029,9 @@ $$
|
||||
=== "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;
|
||||
}
|
||||
[class]{worst_best_time_complexity}-[func]{randomNumbers}
|
||||
|
||||
// 随机打乱数组元素
|
||||
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++)
|
||||
{
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
[class]{worst_best_time_complexity}-[func]{findOne}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
Reference in New Issue
Block a user