mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +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,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),
|
||||
|
||||
Reference in New Issue
Block a user