2022-12-23 18:28:17 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: space_complexity.cs
|
|
|
|
|
* Created Time: 2022-12-23
|
|
|
|
|
* Author: haptear (haptear@hotmail.com)
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
namespace hello_algo.chapter_computational_complexity;
|
|
|
|
|
|
2023-04-23 03:03:12 +08:00
|
|
|
|
public class space_complexity {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 函数 */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int Function() {
|
2023-08-21 03:06:53 +08:00
|
|
|
|
// 执行某些操作
|
2023-02-08 22:18:02 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 常数阶 */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
void Constant(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
|
int a = 0;
|
|
|
|
|
int b = 0;
|
|
|
|
|
int[] nums = new int[10000];
|
2023-10-08 01:33:46 +08:00
|
|
|
|
ListNode node = new(0);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
int c = 0;
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-10-08 01:33:46 +08:00
|
|
|
|
Function();
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 线性阶 */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
void Linear(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
|
int[] nums = new int[n];
|
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
2023-11-26 23:18:44 +08:00
|
|
|
|
List<ListNode> nodes = [];
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
nodes.Add(new ListNode(i));
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
2023-11-26 23:18:44 +08:00
|
|
|
|
Dictionary<int, string> map = [];
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
map.Add(i, i.ToString());
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 线性阶(递归实现) */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
void LinearRecur(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
Console.WriteLine("递归 n = " + n);
|
|
|
|
|
if (n == 1) return;
|
2023-10-08 01:33:46 +08:00
|
|
|
|
LinearRecur(n - 1);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 平方阶 */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
void Quadratic(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 矩阵占用 O(n^2) 空间
|
|
|
|
|
int[,] numMatrix = new int[n, n];
|
|
|
|
|
// 二维列表占用 O(n^2) 空间
|
2023-11-26 23:18:44 +08:00
|
|
|
|
List<List<int>> numList = [];
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-11-26 23:18:44 +08:00
|
|
|
|
List<int> tmp = [];
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int j = 0; j < n; j++) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
tmp.Add(0);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
numList.Add(tmp);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 平方阶(递归实现) */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int QuadraticRecur(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
if (n <= 0) return 0;
|
|
|
|
|
int[] nums = new int[n];
|
|
|
|
|
Console.WriteLine("递归 n = " + n + " 中的 nums 长度 = " + nums.Length);
|
2023-10-08 01:33:46 +08:00
|
|
|
|
return QuadraticRecur(n - 1);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 指数阶(建立满二叉树) */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
TreeNode? BuildTree(int n) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
if (n == 0) return null;
|
2023-10-08 01:33:46 +08:00
|
|
|
|
TreeNode root = new(0) {
|
|
|
|
|
left = BuildTree(n - 1),
|
|
|
|
|
right = BuildTree(n - 1)
|
|
|
|
|
};
|
2023-02-08 22:18:02 +08:00
|
|
|
|
return root;
|
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[Test]
|
2023-04-23 03:03:12 +08:00
|
|
|
|
public void Test() {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
int n = 5;
|
|
|
|
|
// 常数阶
|
2023-10-08 01:33:46 +08:00
|
|
|
|
Constant(n);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 线性阶
|
2023-10-08 01:33:46 +08:00
|
|
|
|
Linear(n);
|
|
|
|
|
LinearRecur(n);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 平方阶
|
2023-10-08 01:33:46 +08:00
|
|
|
|
Quadratic(n);
|
|
|
|
|
QuadraticRecur(n);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 指数阶
|
2023-10-08 01:33:46 +08:00
|
|
|
|
TreeNode? root = BuildTree(n);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
PrintUtil.PrintTree(root);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|