mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
fix(csharp): Modify method name to PascalCase, simplify new expression (#840)
* Modify method name to PascalCase(array and linked list) * Modify method name to PascalCase(backtracking) * Modify method name to PascalCase(computational complexity) * Modify method name to PascalCase(divide and conquer) * Modify method name to PascalCase(dynamic programming) * Modify method name to PascalCase(graph) * Modify method name to PascalCase(greedy) * Modify method name to PascalCase(hashing) * Modify method name to PascalCase(heap) * Modify method name to PascalCase(searching) * Modify method name to PascalCase(sorting) * Modify method name to PascalCase(stack and queue) * Modify method name to PascalCase(tree) * local check
This commit is contained in:
@ -8,7 +8,7 @@ namespace hello_algo.chapter_computational_complexity;
|
||||
|
||||
public class iteration {
|
||||
/* for 循环 */
|
||||
public int forLoop(int n) {
|
||||
public static 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 int whileLoop(int n) {
|
||||
public static int WhileLoop(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 初始化条件变量
|
||||
// 循环求和 1, 2, ..., n-1, n
|
||||
@ -30,7 +30,7 @@ public class iteration {
|
||||
}
|
||||
|
||||
/* while 循环(两次更新) */
|
||||
public int whileLoopII(int n) {
|
||||
public static int WhileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 初始化条件变量
|
||||
// 循环求和 1, 2, 4, 5...
|
||||
@ -44,8 +44,8 @@ public class iteration {
|
||||
}
|
||||
|
||||
/* 双层 for 循环 */
|
||||
public string nestedForLoop(int n) {
|
||||
StringBuilder res = new StringBuilder();
|
||||
public static 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
|
||||
@ -62,16 +62,16 @@ public class iteration {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = forLoop(n);
|
||||
res = ForLoop(n);
|
||||
Console.WriteLine("\nfor 循环的求和结果 res = " + res);
|
||||
|
||||
res = whileLoop(n);
|
||||
res = WhileLoop(n);
|
||||
Console.WriteLine("\nwhile 循环的求和结果 res = " + res);
|
||||
|
||||
res = whileLoopII(n);
|
||||
res = WhileLoopII(n);
|
||||
Console.WriteLine("\nwhile 循环(两次更新)求和结果 res = " + res);
|
||||
|
||||
string resStr = nestedForLoop(n);
|
||||
string resStr = NestedForLoop(n);
|
||||
Console.WriteLine("\n双层 for 循环的遍历结果 " + resStr);
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,20 +8,20 @@ namespace hello_algo.chapter_computational_complexity;
|
||||
|
||||
public class recursion {
|
||||
/* 递归 */
|
||||
public int recur(int n) {
|
||||
public int Recur(int n) {
|
||||
// 终止条件
|
||||
if (n == 1)
|
||||
return 1;
|
||||
// 递:递归调用
|
||||
int res = recur(n - 1);
|
||||
int res = Recur(n - 1);
|
||||
// 归:返回结果
|
||||
return n + res;
|
||||
}
|
||||
|
||||
/* 使用迭代模拟递归 */
|
||||
public int forLoopRecur(int n) {
|
||||
public static int ForLoopRecur(int n) {
|
||||
// 使用一个显式的栈来模拟系统调用栈
|
||||
Stack<int> stack = new Stack<int>();
|
||||
Stack<int> stack = new();
|
||||
int res = 0;
|
||||
// 递:递归调用
|
||||
for (int i = n; i > 0; i--) {
|
||||
@ -38,21 +38,21 @@ public class recursion {
|
||||
}
|
||||
|
||||
/* 尾递归 */
|
||||
public int tailRecur(int n, int res) {
|
||||
public int TailRecur(int n, int res) {
|
||||
// 终止条件
|
||||
if (n == 0)
|
||||
return res;
|
||||
// 尾递归调用
|
||||
return tailRecur(n - 1, res + n);
|
||||
return TailRecur(n - 1, res + n);
|
||||
}
|
||||
|
||||
/* 斐波那契数列:递归 */
|
||||
public int fib(int n) {
|
||||
public 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;
|
||||
}
|
||||
@ -63,16 +63,16 @@ public class recursion {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
res = Recur(n);
|
||||
Console.WriteLine("\n递归函数的求和结果 res = " + res);
|
||||
|
||||
res = forLoopRecur(n);
|
||||
res = ForLoopRecur(n);
|
||||
Console.WriteLine("\n使用迭代模拟递归求和结果 res = " + res);
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
res = TailRecur(n, 0);
|
||||
Console.WriteLine("\n尾递归函数的求和结果 res = " + res);
|
||||
|
||||
res = fib(n);
|
||||
res = Fib(n);
|
||||
Console.WriteLine("\n斐波那契数列的第 " + n + " 项为 " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,30 +8,30 @@ namespace hello_algo.chapter_computational_complexity;
|
||||
|
||||
public class space_complexity {
|
||||
/* 函数 */
|
||||
static int function() {
|
||||
static int Function() {
|
||||
// 执行某些操作
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 常数阶 */
|
||||
static void constant(int n) {
|
||||
static 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();
|
||||
}
|
||||
}
|
||||
|
||||
/* 线性阶 */
|
||||
static void linear(int n) {
|
||||
static void Linear(int n) {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
int[] nums = new int[n];
|
||||
// 长度为 n 的列表占用 O(n) 空间
|
||||
@ -47,14 +47,14 @@ public class space_complexity {
|
||||
}
|
||||
|
||||
/* 线性阶(递归实现) */
|
||||
static void linearRecur(int n) {
|
||||
static void LinearRecur(int n) {
|
||||
Console.WriteLine("递归 n = " + n);
|
||||
if (n == 1) return;
|
||||
linearRecur(n - 1);
|
||||
LinearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
static void quadratic(int n) {
|
||||
static void Quadratic(int n) {
|
||||
// 矩阵占用 O(n^2) 空间
|
||||
int[,] numMatrix = new int[n, n];
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
@ -69,19 +69,20 @@ public class space_complexity {
|
||||
}
|
||||
|
||||
/* 平方阶(递归实现) */
|
||||
static int quadraticRecur(int n) {
|
||||
static 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);
|
||||
}
|
||||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
static TreeNode? buildTree(int n) {
|
||||
static 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;
|
||||
}
|
||||
|
||||
@ -89,15 +90,15 @@ public class space_complexity {
|
||||
public void Test() {
|
||||
int n = 5;
|
||||
// 常数阶
|
||||
constant(n);
|
||||
Constant(n);
|
||||
// 线性阶
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
Linear(n);
|
||||
LinearRecur(n);
|
||||
// 平方阶
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
Quadratic(n);
|
||||
QuadraticRecur(n);
|
||||
// 指数阶
|
||||
TreeNode? root = buildTree(n);
|
||||
TreeNode? root = BuildTree(n);
|
||||
PrintUtil.PrintTree(root);
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
namespace hello_algo.chapter_computational_complexity;
|
||||
|
||||
public class time_complexity {
|
||||
void algorithm(int n) {
|
||||
void Algorithm(int n) {
|
||||
int a = 1; // +0(技巧 1)
|
||||
a = a + n; // +0(技巧 1)
|
||||
// +n(技巧 2)
|
||||
@ -23,24 +23,24 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
// 算法 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);
|
||||
}
|
||||
}
|
||||
|
||||
/* 常数阶 */
|
||||
static int constant(int n) {
|
||||
static int Constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
@ -49,7 +49,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 线性阶 */
|
||||
static int linear(int n) {
|
||||
static int Linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
@ -57,7 +57,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 线性阶(遍历数组) */
|
||||
static int arrayTraversal(int[] nums) {
|
||||
static int ArrayTraversal(int[] nums) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
foreach (int num in nums) {
|
||||
@ -67,7 +67,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
static int quadratic(int n) {
|
||||
static int Quadratic(int n) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
for (int i = 0; i < n; i++) {
|
||||
@ -79,7 +79,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 平方阶(冒泡排序) */
|
||||
static int bubbleSort(int[] nums) {
|
||||
static int BubbleSort(int[] nums) {
|
||||
int count = 0; // 计数器
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
@ -96,7 +96,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 指数阶(循环实现) */
|
||||
static int exponential(int n) {
|
||||
static int Exponential(int n) {
|
||||
int count = 0, bas = 1;
|
||||
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for (int i = 0; i < n; i++) {
|
||||
@ -110,13 +110,13 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 指数阶(递归实现) */
|
||||
static int expRecur(int n) {
|
||||
static 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;
|
||||
}
|
||||
|
||||
/* 对数阶(循环实现) */
|
||||
static int logarithmic(float n) {
|
||||
static int Logarithmic(float n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
@ -126,16 +126,16 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
static int logRecur(float n) {
|
||||
static int LogRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
return LogRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 线性对数阶 */
|
||||
static int linearLogRecur(float n) {
|
||||
static 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++;
|
||||
}
|
||||
@ -143,12 +143,12 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 阶乘阶(递归实现) */
|
||||
static int factorialRecur(int n) {
|
||||
static 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;
|
||||
}
|
||||
@ -159,36 +159,36 @@ public class time_complexity {
|
||||
int n = 8;
|
||||
Console.WriteLine("输入数据大小 n = " + n);
|
||||
|
||||
int count = constant(n);
|
||||
int count = Constant(n);
|
||||
Console.WriteLine("常数阶的操作数量 = " + count);
|
||||
|
||||
count = linear(n);
|
||||
count = Linear(n);
|
||||
Console.WriteLine("线性阶的操作数量 = " + count);
|
||||
count = arrayTraversal(new int[n]);
|
||||
count = ArrayTraversal(new int[n]);
|
||||
Console.WriteLine("线性阶(遍历数组)的操作数量 = " + count);
|
||||
|
||||
count = quadratic(n);
|
||||
count = Quadratic(n);
|
||||
Console.WriteLine("平方阶的操作数量 = " + count);
|
||||
int[] nums = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = bubbleSort(nums);
|
||||
count = BubbleSort(nums);
|
||||
Console.WriteLine("平方阶(冒泡排序)的操作数量 = " + count);
|
||||
|
||||
count = exponential(n);
|
||||
count = Exponential(n);
|
||||
Console.WriteLine("指数阶(循环实现)的操作数量 = " + count);
|
||||
count = expRecur(n);
|
||||
count = ExpRecur(n);
|
||||
Console.WriteLine("指数阶(递归实现)的操作数量 = " + count);
|
||||
|
||||
count = logarithmic((float)n);
|
||||
count = Logarithmic((float)n);
|
||||
Console.WriteLine("对数阶(循环实现)的操作数量 = " + count);
|
||||
count = logRecur((float)n);
|
||||
count = LogRecur((float)n);
|
||||
Console.WriteLine("对数阶(递归实现)的操作数量 = " + count);
|
||||
|
||||
count = linearLogRecur((float)n);
|
||||
count = LinearLogRecur((float)n);
|
||||
Console.WriteLine("线性对数阶(递归实现)的操作数量 = " + count);
|
||||
|
||||
count = factorialRecur(n);
|
||||
count = FactorialRecur(n);
|
||||
Console.WriteLine("阶乘阶(递归实现)的操作数量 = " + count);
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@ namespace hello_algo.chapter_computational_complexity;
|
||||
|
||||
public class worst_best_time_complexity {
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
static int[] randomNumbers(int n) {
|
||||
static int[] RandomNumbers(int n) {
|
||||
int[] nums = new int[n];
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
for (int i = 0; i < n; i++) {
|
||||
@ -27,7 +27,7 @@ public class worst_best_time_complexity {
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
static int findOne(int[] nums) {
|
||||
static int FindOne(int[] nums) {
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
@ -43,8 +43,8 @@ public class worst_best_time_complexity {
|
||||
public void Test() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int n = 100;
|
||||
int[] nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
int[] nums = RandomNumbers(n);
|
||||
int index = FindOne(nums);
|
||||
Console.WriteLine("\n数组 [ 1, 2, ..., n ] 被打乱后 = " + string.Join(",", nums));
|
||||
Console.WriteLine("数字 1 的索引为 " + index);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user