Add space_complexit under C and fix memory leak under CPP (#456)
* fix(codes/cpp): Memory leak fix: the space was not freed when pop removed the element. * fix(codes/cpp): Fix access error when printArray(arr, 0) * fix(codes/cpp): Fix memory leaks: replace pointers with local variables, no need to manage memory * fix(codes/cpp): Fix memory leaks: no delete * fix(codes/cpp): Fix memory leaks: Add destructor ~ArrayHashMap() * Update PrintUtil.hpp * feat(codes/c): Add three-party hash implementation * feat(codes/c): Add freeMemoryTree in tree_node.h * feat(codes/c): Add space_complexity.c * styles(codes/c): Modify format * feat(codes/cpp): Undo a previous delete, there is no memory leak here * Update array_hash_map.cpp * Update include.h --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
parent
c8344e8636
commit
8bc41bc013
@ -1,3 +1,4 @@
|
|||||||
add_executable(time_complexity time_complexity.c )
|
add_executable(time_complexity time_complexity.c)
|
||||||
add_executable(worst_best_time_complexity worst_best_time_complexity.c)
|
add_executable(worst_best_time_complexity worst_best_time_complexity.c)
|
||||||
add_executable(leetcode_two_sum leetcode_two_sum.c)
|
add_executable(leetcode_two_sum leetcode_two_sum.c)
|
||||||
|
add_executable(space_complexity space_complexity.c)
|
139
codes/c/chapter_computational_complexity/space_complexity.c
Normal file
139
codes/c/chapter_computational_complexity/space_complexity.c
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
/**
|
||||||
|
* File: space_complexity.c
|
||||||
|
* Created Time: 2023-04-15
|
||||||
|
* Author: Gonglja (glj0@outlook.com)
|
||||||
|
*/
|
||||||
|
#include "../include/include.h"
|
||||||
|
|
||||||
|
/* 函数 */
|
||||||
|
int func() {
|
||||||
|
// do something
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 常数阶 */
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int key;
|
||||||
|
char val[10];
|
||||||
|
UT_hash_handle hh; // 借助 uthash 实现的哈希表
|
||||||
|
} hashTable;
|
||||||
|
|
||||||
|
/* 线性阶 */
|
||||||
|
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;
|
||||||
|
sprintf(tmp->val, "%d", i);
|
||||||
|
HASH_ADD_INT(h, key, tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 内存释放
|
||||||
|
hashTable *curr, *tmp;
|
||||||
|
HASH_ITER(hh, h, curr, tmp) {
|
||||||
|
HASH_DEL(h, curr);
|
||||||
|
free(curr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 线性阶(递归实现) */
|
||||||
|
void linearRecur(int n) {
|
||||||
|
printf("递归 n = %d\r\n", n);
|
||||||
|
if (n == 1)
|
||||||
|
return;
|
||||||
|
linearRecur(n - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 平方阶 */
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 平方阶(递归实现) */
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 指数阶(建立满二叉树) */
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Driver Code */
|
||||||
|
int main() {
|
||||||
|
int n = 5;
|
||||||
|
// 常数阶
|
||||||
|
constant(n);
|
||||||
|
// 线性阶
|
||||||
|
linear(n);
|
||||||
|
linearRecur(n);
|
||||||
|
// 平方阶
|
||||||
|
quadratic(n);
|
||||||
|
quadraticRecur(n);
|
||||||
|
// 指数阶
|
||||||
|
TreeNode *root = buildTree(n);
|
||||||
|
printTree(root);
|
||||||
|
|
||||||
|
// 释放内存
|
||||||
|
freeMemoryTree(root);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
add_executable(include
|
add_executable(include
|
||||||
include_test.c
|
include_test.c
|
||||||
include.h print_util.h
|
include.h print_util.h
|
||||||
list_node.h tree_node.h)
|
list_node.h tree_node.h
|
||||||
|
uthash.h)
|
@ -18,6 +18,9 @@
|
|||||||
#include "tree_node.h"
|
#include "tree_node.h"
|
||||||
#include "print_util.h"
|
#include "print_util.h"
|
||||||
|
|
||||||
|
// hash table lib
|
||||||
|
#include "uthash.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
@ -124,6 +124,15 @@ int *treeToArr(TreeNode *root) {
|
|||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Free the memory allocated to a tree */
|
||||||
|
void freeMemoryTree(TreeNode *root) {
|
||||||
|
if (root == NULL)
|
||||||
|
return;
|
||||||
|
freeMemoryTree(root->left);
|
||||||
|
freeMemoryTree(root->right);
|
||||||
|
// 释放内存
|
||||||
|
free(root);
|
||||||
|
}
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
1140
codes/c/include/uthash.h
Normal file
1140
codes/c/include/uthash.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -28,6 +28,14 @@ class ArrayHashMap {
|
|||||||
buckets = vector<Entry *>(100);
|
buckets = vector<Entry *>(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
~ArrayHashMap() {
|
||||||
|
// 释放内存
|
||||||
|
for (const auto &bucket : buckets) {
|
||||||
|
delete bucket;
|
||||||
|
}
|
||||||
|
buckets.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/* 哈希函数 */
|
/* 哈希函数 */
|
||||||
int hashFunc(int key) {
|
int hashFunc(int key) {
|
||||||
int index = key % 100;
|
int index = key % 100;
|
||||||
@ -53,7 +61,8 @@ class ArrayHashMap {
|
|||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
void remove(int key) {
|
void remove(int key) {
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
// 置为 nullptr ,代表删除
|
// 释放内存并置为 nullptr
|
||||||
|
delete buckets[index];
|
||||||
buckets[index] = nullptr;
|
buckets[index] = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user