This commit is contained in:
krahets
2023-02-11 18:21:44 +08:00
parent 76dcc6cbd3
commit 58ad2290b0
20 changed files with 418 additions and 219 deletions
@@ -706,7 +706,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceConstant}
```
=== "C#"
@@ -902,7 +902,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceLinear}
```
=== "C#"
@@ -1041,7 +1041,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceLinearRecur}
```
=== "C#"
@@ -1187,7 +1187,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceQuadratic}
```
=== "C#"
@@ -1322,7 +1322,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceQuadraticRecur}
```
=== "C#"
@@ -1456,7 +1456,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{buildTree}
```
=== "C#"
@@ -134,7 +134,7 @@ comments: true
=== "C"
```c title="leetcode_two_sum.c"
[class]{}-[func]{twoSumBruteForce}
```
=== "C#"
@@ -313,7 +313,7 @@ comments: true
=== "C"
```c title="leetcode_two_sum.c"
[class]{}-[func]{twoSumHashTable}
```
=== "C#"
@@ -871,16 +871,7 @@ $$
=== "C"
```c title="time_complexity.c"
/* 常数阶 */
int constant(int n) {
int count = 0;
int size = 100000;
int i = 0;
for (int i = 0; i < size; i++) {
count ++;
}
return count;
}
[class]{}-[func]{constant}
```
=== "C#"
@@ -1004,14 +995,7 @@ $$
=== "C"
```c title="time_complexity.c"
/* 线性阶 */
int linear(int n) {
int count = 0;
for (int i = 0; i < n; i++) {
count ++;
}
return count;
}
[class]{}-[func]{linear}
```
=== "C#"
@@ -1145,15 +1129,7 @@ $$
=== "C"
```c title="time_complexity.c"
/* 线性阶(遍历数组) */
int arrayTraversal(int *nums, int n) {
int count = 0;
// 循环次数与数组长度成正比
for (int i = 0; i < n; i++) {
count ++;
}
return count;
}
[class]{}-[func]{arrayTraversal}
```
=== "C#"
@@ -1300,17 +1276,7 @@ $$
=== "C"
```c title="time_complexity.c"
/* 平方阶 */
int quadratic(int n) {
int count = 0;
// 循环次数与数组长度成平方关系
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count ++;
}
}
return count;
}
[class]{}-[func]{quadratic}
```
=== "C#"
@@ -1513,26 +1479,7 @@ $$
=== "C"
```c title="time_complexity.c"
/* 平方阶(冒泡排序) */
int bubbleSort(int *nums, int n) {
int count = 0; // 计数器
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (int i = n - 1; i > 0; i--) {
// 内循环:冒泡操作
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];
nums[j + 1] = tmp;
count += 3; // 元素交换包含 3 个单元操作
}
}
}
return count;
}
[class]{}-[func]{bubbleSort}
```
=== "C#"
@@ -1728,20 +1675,7 @@ $$
=== "C"
```c title="time_complexity.c"
/* 指数阶(循环实现) */
int exponential(int n) {
int count = 0;
int bas = 1;
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++) {
for (int j = 0; j < bas; j++) {
count++;
}
bas *= 2;
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count;
}
[class]{}-[func]{exponential}
```
=== "C#"
@@ -1875,11 +1809,7 @@ $$
=== "C"
```c title="time_complexity.c"
/* 指数阶(递归实现) */
int expRecur(int n) {
if (n == 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
[class]{}-[func]{expRecur}
```
=== "C#"
@@ -2008,15 +1938,7 @@ $$
=== "C"
```c title="time_complexity.c"
/* 对数阶(循环实现) */
int logarithmic(float n) {
int count = 0;
while (n > 1) {
n = n / 2;
count++;
}
return count;
}
[class]{}-[func]{logarithmic}
```
=== "C#"
@@ -2136,11 +2058,7 @@ $$
=== "C"
```c title="time_complexity.c"
/* 对数阶(递归实现) */
int logRecur(float n) {
if (n <= 1) return 0;
return logRecur(n / 2) + 1;
}
[class]{}-[func]{logRecur}
```
=== "C#"
@@ -2273,16 +2191,7 @@ $$
=== "C"
```c title="time_complexity.c"
/* 线性对数阶 */
int linearLogRecur(float n) {
if (n <= 1) return 1;
int count = linearLogRecur(n / 2) +
linearLogRecur(n / 2);
for (int i = 0; i < n; i++) {
count ++;
}
return count;
}
[class]{}-[func]{linearLogRecur}
```
=== "C#"
@@ -2441,15 +2350,7 @@ $$
=== "C"
```c title="time_complexity.c"
/* 阶乘阶(递归实现) */
int factorialRecur(int n) {
if (n == 0) return 1;
int count = 0;
for (int i = 0; i < n; i++) {
count += factorialRecur(n - 1);
}
return count;
}
[class]{}-[func]{factorialRecur}
```
=== "C#"