This commit is contained in:
krahets
2023-04-23 14:58:03 +08:00
parent 881ece517f
commit fe8027e64a
26 changed files with 344 additions and 626 deletions
@@ -760,21 +760,18 @@ $$
```csharp title="space_complexity.cs"
/* 常数阶 */
void constant(int n)
{
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++)
{
for (int i = 0; i < n; i++) {
int c = 0;
}
// 循环中的函数占用 O(1) 空间
for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
function();
}
}
@@ -997,20 +994,17 @@ $$
```csharp title="space_complexity.cs"
/* 线性阶 */
void linear(int n)
{
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++)
{
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++)
{
for (int i = 0; i < n; i++) {
map.Add(i, i.ToString());
}
}
@@ -1145,8 +1139,7 @@ $$
```csharp title="space_complexity.cs"
/* 线性阶(递归实现) */
void linearRecur(int n)
{
void linearRecur(int n) {
Console.WriteLine("递归 n = " + n);
if (n == 1) return;
linearRecur(n - 1);
@@ -1312,17 +1305,14 @@ $$
```csharp title="space_complexity.cs"
/* 平方阶 */
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();
for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
List<int> tmp = new();
for (int j = 0; j < n; j++)
{
for (int j = 0; j < n; j++) {
tmp.Add(0);
}
numList.Add(tmp);
@@ -1458,8 +1448,7 @@ $$
```csharp title="space_complexity.cs"
/* 平方阶(递归实现) */
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);
@@ -1603,8 +1592,7 @@ $$
```csharp title="space_complexity.cs"
/* 指数阶(建立满二叉树) */
TreeNode? buildTree(int n)
{
TreeNode? buildTree(int n) {
if (n == 0) return null;
TreeNode root = new TreeNode(0);
root.left = buildTree(n - 1);
@@ -885,8 +885,7 @@ $$
```csharp title="time_complexity.cs"
/* 常数阶 */
int constant(int n)
{
int constant(int n) {
int count = 0;
int size = 100000;
for (int i = 0; i < size; i++)
@@ -1016,8 +1015,7 @@ $$
```csharp title="time_complexity.cs"
/* 线性阶 */
int linear(int n)
{
int linear(int n) {
int count = 0;
for (int i = 0; i < n; i++)
count++;
@@ -1158,12 +1156,10 @@ $$
```csharp title="time_complexity.cs"
/* 线性阶(遍历数组) */
int arrayTraversal(int[] nums)
{
int arrayTraversal(int[] nums) {
int count = 0;
// 循环次数与数组长度成正比
foreach (int num in nums)
{
foreach (int num in nums) {
count++;
}
return count;
@@ -1315,14 +1311,11 @@ $$
```csharp title="time_complexity.cs"
/* 平方阶 */
int quadratic(int n)
{
int quadratic(int n) {
int count = 0;
// 循环次数与数组长度成平方关系
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count++;
}
}
@@ -1535,17 +1528,13 @@ $$
```csharp title="time_complexity.cs"
/* 平方阶(冒泡排序) */
int bubbleSort(int[] nums)
{
int bubbleSort(int[] nums) {
int count = 0; // 计数器
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (int i = nums.Length - 1; i > 0; i--)
{
for (int i = nums.Length - 1; i > 0; i--) {
// 内循环:冒泡操作
for (int j = 0; j < i; j++)
{
if (nums[j] > nums[j + 1])
{
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];
@@ -1745,14 +1734,11 @@ $$
```csharp title="time_complexity.cs"
/* 指数阶(循环实现) */
int exponential(int n)
{
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++)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < bas; j++) {
count++;
}
bas *= 2;
@@ -1887,8 +1873,7 @@ $$
```csharp title="time_complexity.cs"
/* 指数阶(递归实现) */
int expRecur(int n)
{
int expRecur(int n) {
if (n == 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
@@ -2024,11 +2009,9 @@ $$
```csharp title="time_complexity.cs"
/* 对数阶(循环实现) */
int logarithmic(float n)
{
int logarithmic(float n) {
int count = 0;
while (n > 1)
{
while (n > 1) {
n = n / 2;
count++;
}
@@ -2152,8 +2135,7 @@ $$
```csharp title="time_complexity.cs"
/* 对数阶(递归实现) */
int logRecur(float n)
{
int logRecur(float n) {
if (n <= 1) return 0;
return logRecur(n / 2) + 1;
}
@@ -2295,13 +2277,11 @@ $$
```csharp title="time_complexity.cs"
/* 线性对数阶 */
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++)
{
for (int i = 0; i < n; i++) {
count++;
}
return count;
@@ -2466,13 +2446,11 @@ $$
```csharp title="time_complexity.cs"
/* 阶乘阶(递归实现) */
int factorialRecur(int n)
{
int factorialRecur(int n) {
if (n == 0) return 1;
int count = 0;
// 从 1 个分裂出 n 个
for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
count += factorialRecur(n - 1);
}
return count;
@@ -2740,18 +2718,15 @@ $$
```csharp title="worst_best_time_complexity.cs"
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
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++)
{
for (int i = 0; i < n; i++) {
nums[i] = i + 1;
}
// 随机打乱数组元素
for (int i = 0; i < nums.Length; i++)
{
for (int i = 0; i < nums.Length; i++) {
var index = new Random().Next(i, nums.Length);
var tmp = nums[i];
var ran = nums[index];
@@ -2762,10 +2737,8 @@ $$
}
/* 查找数组 nums 中数字 1 所在索引 */
int findOne(int[] nums)
{
for (int i = 0; i < nums.Length; i++)
{
int findOne(int[] nums) {
for (int i = 0; i < nums.Length; i++) {
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
if (nums[i] == 1)