This commit is contained in:
krahets
2023-04-18 20:19:07 +08:00
parent cf4a59e3d6
commit 363f1f4b5f
25 changed files with 1910 additions and 107 deletions
@@ -188,7 +188,18 @@ comments: true
=== "C"
```c title=""
/* 函数 */
int func() {
// do something...
return 0;
}
int algorithm(int n) { // 输入数据
const int a = 0; // 暂存数据(常量)
int b = 0; // 暂存数据(变量)
int c = func(); // 栈帧空间(调用函数)
return a + b + c; // 输出数据
}
```
=== "C#"
@@ -335,7 +346,12 @@ comments: true
=== "C"
```c title=""
void algorithm(int n) {
int a = 0; // O(1)
int b[10000]; // O(1)
if (n > 10)
vector<int> nums(n); // O(n)
}
```
=== "C#"
@@ -497,7 +513,21 @@ comments: true
=== "C"
```c title=""
int func() {
// do something
return 0;
}
/* 循环 O(1) */
void loop(int n) {
for (int i = 0; i < n; i++) {
func();
}
}
/* 递归 O(n) */
void recur(int n) {
if (n == 1) return;
return recur(n - 1);
}
```
=== "C#"
@@ -707,7 +737,23 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceConstant}
/* 常数阶 */
void constant(int n) {
// 常量、变量、对象占用 O(1) 空间
const int a = 0;
int b = 0;
int nums[1000];
ListNode *node = newListNode(0);
free(node);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++) {
int c = 0;
}
// 循环中的函数占用 O(1) 空间
for (int i = 0; i < n; i++) {
func();
}
}
```
=== "C#"
@@ -903,7 +949,40 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceLinear}
/* 线性阶 */
void linear(int n) {
// 长度为 n 的数组占用 O(n) 空间
int *nums = malloc(sizeof(int) * n);
free(nums);
// 长度为 n 的列表占用 O(n) 空间
ListNode **nodes = malloc(sizeof(ListNode *) * n);
for (int i = 0; i < n; i++) {
nodes[i] = newListNode(i);
}
// 内存释放
for (int i = 0; i < n; i++) {
free(nodes[i]);
}
free(nodes);
// 长度为 n 的哈希表占用 O(n) 空间
hashTable *h = NULL;
for (int i = 0; i < n; i++) {
hashTable *tmp = malloc(sizeof(hashTable));
tmp->key = i;
tmp->val = i;
HASH_ADD_INT(h, key, tmp);
}
// 内存释放
hashTable *curr, *tmp;
HASH_ITER(hh, h, curr, tmp) {
HASH_DEL(h, curr);
free(curr);
}
}
```
=== "C#"
@@ -1045,7 +1124,13 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceLinearRecur}
/* 线性阶(递归实现) */
void linearRecur(int n) {
printf("递归 n = %d\r\n", n);
if (n == 1)
return;
linearRecur(n - 1);
}
```
=== "C#"
@@ -1195,7 +1280,24 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceQuadratic}
/* 平方阶 */
void quadratic(int n) {
// 二维列表占用 O(n^2) 空间
int **numMatrix = malloc(sizeof(int *) * n);
for (int i = 0; i < n; i++) {
int *tmp = malloc(sizeof(int) * n);
for (int j = 0; j < n; j++) {
tmp[j] = 0;
}
numMatrix[i] = tmp;
}
// 内存释放
for (int i = 0; i < n; i++) {
free(numMatrix[i]);
}
free(numMatrix);
}
```
=== "C#"
@@ -1333,7 +1435,15 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceQuadraticRecur}
/* 平方阶(递归实现) */
int quadraticRecur(int n) {
if (n <= 0)
return 0;
int *nums = malloc(sizeof(int) * n);
printf("递归 n = %d 中的 nums 长度 = %d\r\n", n, n);
free(nums);
return quadraticRecur(n - 1);
}
```
=== "C#"
@@ -1470,7 +1580,15 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{buildTree}
/* 指数阶(建立满二叉树) */
TreeNode *buildTree(int n) {
if (n == 0)
return NULL;
TreeNode *root = newTreeNode(0);
root->left = buildTree(n - 1);
root->right = buildTree(n - 1);
return root;
}
```
=== "C#"
@@ -869,7 +869,16 @@ $$
=== "C"
```c title="time_complexity.c"
[class]{}-[func]{constant}
/* 常数阶 */
int constant(int n) {
int count = 0;
int size = 100000;
int i = 0;
for (int i = 0; i < size; i++) {
count++;
}
return count;
}
```
=== "C#"
@@ -993,7 +1002,14 @@ $$
=== "C"
```c title="time_complexity.c"
[class]{}-[func]{linear}
/* 线性阶 */
int linear(int n) {
int count = 0;
for (int i = 0; i < n; i++) {
count++;
}
return count;
}
```
=== "C#"
@@ -1127,7 +1143,15 @@ $$
=== "C"
```c title="time_complexity.c"
[class]{}-[func]{arrayTraversal}
/* 线性阶(遍历数组) */
int arrayTraversal(int *nums, int n) {
int count = 0;
// 循环次数与数组长度成正比
for (int i = 0; i < n; i++) {
count++;
}
return count;
}
```
=== "C#"
@@ -1274,7 +1298,17 @@ $$
=== "C"
```c title="time_complexity.c"
[class]{}-[func]{quadratic}
/* 平方阶 */
int quadratic(int n) {
int count = 0;
// 循环次数与数组长度成平方关系
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count++;
}
}
return count;
}
```
=== "C#"
@@ -1477,7 +1511,24 @@ $$
=== "C"
```c title="time_complexity.c"
[class]{}-[func]{bubbleSort}
/* 平方阶(冒泡排序) */
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;
}
```
=== "C#"
@@ -1674,7 +1725,20 @@ $$
=== "C"
```c title="time_complexity.c"
[class]{}-[func]{exponential}
/* 指数阶(循环实现) */
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;
}
```
=== "C#"
@@ -1811,7 +1875,12 @@ $$
=== "C"
```c title="time_complexity.c"
[class]{}-[func]{expRecur}
/* 指数阶(递归实现) */
int expRecur(int n) {
if (n == 1)
return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
```
=== "C#"
@@ -1940,7 +2009,15 @@ $$
=== "C"
```c title="time_complexity.c"
[class]{}-[func]{logarithmic}
/* 对数阶(循环实现) */
int logarithmic(float n) {
int count = 0;
while (n > 1) {
n = n / 2;
count++;
}
return count;
}
```
=== "C#"
@@ -2063,7 +2140,12 @@ $$
=== "C"
```c title="time_complexity.c"
[class]{}-[func]{logRecur}
/* 对数阶(递归实现) */
int logRecur(float n) {
if (n <= 1)
return 0;
return logRecur(n / 2) + 1;
}
```
=== "C#"
@@ -2197,7 +2279,16 @@ $$
=== "C"
```c title="time_complexity.c"
[class]{}-[func]{linearLogRecur}
/* 线性对数阶 */
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;
}
```
=== "C#"
@@ -2359,7 +2450,16 @@ $$
=== "C"
```c title="time_complexity.c"
[class]{}-[func]{factorialRecur}
/* 阶乘阶(递归实现) */
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;
}
```
=== "C#"
@@ -2606,9 +2706,34 @@ $$
=== "C"
```c title="worst_best_time_complexity.c"
[class]{}-[func]{randomNumbers}
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
int *randomNumbers(int n) {
// 分配堆区内存(创建一维可变长数组:数组中元素数量为n,元素类型为int)
int *nums = (int *)malloc(n * sizeof(int));
// 生成数组 nums = { 1, 2, 3, ..., n }
for (int i = 0; i < n; i++) {
nums[i] = i + 1;
}
// 随机打乱数组元素
for (int i = n - 1; i > 0; i--) {
int j = rand() % (i + 1);
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
return nums;
}
[class]{}-[func]{findOne}
/* 查找数组 nums 中数字 1 所在索引 */
int findOne(int *nums, int n) {
for (int i = 0; i < n; i++) {
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
if (nums[i] == 1)
return i;
}
return -1;
}
```
=== "C#"