This commit is contained in:
krahets
2023-10-18 02:16:55 +08:00
parent 64c5d13051
commit d2ba55fcd6
22 changed files with 374 additions and 436 deletions

View File

@@ -1254,13 +1254,11 @@ $$
```c title="space_complexity.c"
/* 哈希表 */
struct hashTable {
typedef struct {
int key;
int val;
UT_hash_handle hh; // 基于 uthash.h 实现
};
typedef struct hashTable hashTable;
} HashTable;
/* 线性阶 */
void linear(int n) {
@@ -1280,16 +1278,16 @@ $$
free(nodes);
// 长度为 n 的哈希表占用 O(n) 空间
hashTable *h = NULL;
HashTable *h = NULL;
for (int i = 0; i < n; i++) {
hashTable *tmp = malloc(sizeof(hashTable));
HashTable *tmp = malloc(sizeof(HashTable));
tmp->key = i;
tmp->val = i;
HASH_ADD_INT(h, key, tmp);
}
// 内存释放
hashTable *curr, *tmp;
HashTable *curr, *tmp;
HASH_ITER(hh, h, curr, tmp) {
HASH_DEL(h, curr);
free(curr);