mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
feat(csharp) .NET 8.0 code migration (#966)
* .net 8.0 migration * update docs * revert change * revert change and update appendix docs * remove static * Update binary_search_insertion.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_computational_complexity;
|
||||
|
||||
public class iteration {
|
||||
/* for 循环 */
|
||||
public static int ForLoop(int n) {
|
||||
int ForLoop(int n) {
|
||||
int res = 0;
|
||||
// 循环求和 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; i++) {
|
||||
@@ -18,7 +18,7 @@ public class iteration {
|
||||
}
|
||||
|
||||
/* while 循环 */
|
||||
public static int WhileLoop(int n) {
|
||||
int WhileLoop(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 初始化条件变量
|
||||
// 循环求和 1, 2, ..., n-1, n
|
||||
@@ -30,7 +30,7 @@ public class iteration {
|
||||
}
|
||||
|
||||
/* while 循环(两次更新) */
|
||||
public static int WhileLoopII(int n) {
|
||||
int WhileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 初始化条件变量
|
||||
// 循环求和 1, 2, 4, 5...
|
||||
@@ -44,7 +44,7 @@ public class iteration {
|
||||
}
|
||||
|
||||
/* 双层 for 循环 */
|
||||
public static string NestedForLoop(int n) {
|
||||
string NestedForLoop(int n) {
|
||||
StringBuilder res = new();
|
||||
// 循环 i = 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; i++) {
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_computational_complexity;
|
||||
|
||||
public class recursion {
|
||||
/* 递归 */
|
||||
public int Recur(int n) {
|
||||
int Recur(int n) {
|
||||
// 终止条件
|
||||
if (n == 1)
|
||||
return 1;
|
||||
@@ -19,7 +19,7 @@ public class recursion {
|
||||
}
|
||||
|
||||
/* 使用迭代模拟递归 */
|
||||
public static int ForLoopRecur(int n) {
|
||||
int ForLoopRecur(int n) {
|
||||
// 使用一个显式的栈来模拟系统调用栈
|
||||
Stack<int> stack = new();
|
||||
int res = 0;
|
||||
@@ -38,7 +38,7 @@ public class recursion {
|
||||
}
|
||||
|
||||
/* 尾递归 */
|
||||
public int TailRecur(int n, int res) {
|
||||
int TailRecur(int n, int res) {
|
||||
// 终止条件
|
||||
if (n == 0)
|
||||
return res;
|
||||
@@ -47,7 +47,7 @@ public class recursion {
|
||||
}
|
||||
|
||||
/* 斐波那契数列:递归 */
|
||||
public int Fib(int n) {
|
||||
int Fib(int n) {
|
||||
// 终止条件 f(1) = 0, f(2) = 1
|
||||
if (n == 1 || n == 2)
|
||||
return n - 1;
|
||||
|
||||
@@ -8,13 +8,13 @@ namespace hello_algo.chapter_computational_complexity;
|
||||
|
||||
public class space_complexity {
|
||||
/* 函数 */
|
||||
static int Function() {
|
||||
int Function() {
|
||||
// 执行某些操作
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 常数阶 */
|
||||
static void Constant(int n) {
|
||||
void Constant(int n) {
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
@@ -31,36 +31,36 @@ public class space_complexity {
|
||||
}
|
||||
|
||||
/* 线性阶 */
|
||||
static void Linear(int n) {
|
||||
void Linear(int n) {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
int[] nums = new int[n];
|
||||
// 长度为 n 的列表占用 O(n) 空间
|
||||
List<ListNode> nodes = new();
|
||||
List<ListNode> nodes = [];
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.Add(new ListNode(i));
|
||||
}
|
||||
// 长度为 n 的哈希表占用 O(n) 空间
|
||||
Dictionary<int, string> map = new();
|
||||
Dictionary<int, string> map = [];
|
||||
for (int i = 0; i < n; i++) {
|
||||
map.Add(i, i.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/* 线性阶(递归实现) */
|
||||
static void LinearRecur(int n) {
|
||||
void LinearRecur(int n) {
|
||||
Console.WriteLine("递归 n = " + n);
|
||||
if (n == 1) return;
|
||||
LinearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
static void Quadratic(int n) {
|
||||
void Quadratic(int n) {
|
||||
// 矩阵占用 O(n^2) 空间
|
||||
int[,] numMatrix = new int[n, n];
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
List<List<int>> numList = new();
|
||||
List<List<int>> numList = [];
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<int> tmp = new();
|
||||
List<int> tmp = [];
|
||||
for (int j = 0; j < n; j++) {
|
||||
tmp.Add(0);
|
||||
}
|
||||
@@ -69,7 +69,7 @@ public class space_complexity {
|
||||
}
|
||||
|
||||
/* 平方阶(递归实现) */
|
||||
static 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);
|
||||
@@ -77,7 +77,7 @@ public class space_complexity {
|
||||
}
|
||||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
static TreeNode? BuildTree(int n) {
|
||||
TreeNode? BuildTree(int n) {
|
||||
if (n == 0) return null;
|
||||
TreeNode root = new(0) {
|
||||
left = BuildTree(n - 1),
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace hello_algo.chapter_computational_complexity;
|
||||
public class time_complexity {
|
||||
void Algorithm(int n) {
|
||||
int a = 1; // +0(技巧 1)
|
||||
a = a + n; // +0(技巧 1)
|
||||
a += n; // +0(技巧 1)
|
||||
// +n(技巧 2)
|
||||
for (int i = 0; i < 5 * n + 1; i++) {
|
||||
Console.WriteLine(0);
|
||||
@@ -26,12 +26,14 @@ public class time_complexity {
|
||||
void AlgorithmA(int n) {
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
|
||||
// 算法 B 时间复杂度:线性阶
|
||||
void AlgorithmB(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
}
|
||||
|
||||
// 算法 C 时间复杂度:常数阶
|
||||
void AlgorithmC(int n) {
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
@@ -40,7 +42,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 常数阶 */
|
||||
static int Constant(int n) {
|
||||
int Constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
@@ -49,7 +51,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 线性阶 */
|
||||
static int Linear(int n) {
|
||||
int Linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
@@ -57,7 +59,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 线性阶(遍历数组) */
|
||||
static int ArrayTraversal(int[] nums) {
|
||||
int ArrayTraversal(int[] nums) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
foreach (int num in nums) {
|
||||
@@ -67,7 +69,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
static int Quadratic(int n) {
|
||||
int Quadratic(int n) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -79,7 +81,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 平方阶(冒泡排序) */
|
||||
static int BubbleSort(int[] nums) {
|
||||
int BubbleSort(int[] nums) {
|
||||
int count = 0; // 计数器
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
@@ -96,7 +98,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 指数阶(循环实现) */
|
||||
static 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++) {
|
||||
@@ -110,29 +112,29 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 指数阶(递归实现) */
|
||||
static int ExpRecur(int n) {
|
||||
int ExpRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* 对数阶(循环实现) */
|
||||
static int Logarithmic(float n) {
|
||||
int Logarithmic(float n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
n /= 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
static int LogRecur(float n) {
|
||||
int LogRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
return LogRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 线性对数阶 */
|
||||
static int LinearLogRecur(float n) {
|
||||
int LinearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -142,7 +144,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 阶乘阶(递归实现) */
|
||||
static int FactorialRecur(int n) {
|
||||
int FactorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
int count = 0;
|
||||
// 从 1 个分裂出 n 个
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_computational_complexity;
|
||||
|
||||
public class worst_best_time_complexity {
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
static 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++) {
|
||||
@@ -24,7 +24,7 @@ public class worst_best_time_complexity {
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
static int FindOne(int[] nums) {
|
||||
int FindOne(int[] nums) {
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
|
||||
Reference in New Issue
Block a user