From 661603d467988da3fb3bcdfa6a99818f94740ecb Mon Sep 17 00:00:00 2001 From: krahets Date: Tue, 18 Apr 2023 21:19:57 +0800 Subject: [PATCH] Add the hashtable based on uthash.h to docs --- .../space_complexity.c | 10 +++++----- codes/c/chapter_searching/hashing_search.c | 6 +++--- codes/c/chapter_searching/leetcode_two_sum.c | 3 +-- .../space_complexity.md | 2 ++ docs/chapter_searching/replace_linear_by_hashing.md | 2 ++ 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/codes/c/chapter_computational_complexity/space_complexity.c b/codes/c/chapter_computational_complexity/space_complexity.c index 0d059b5be..111ea2465 100644 --- a/codes/c/chapter_computational_complexity/space_complexity.c +++ b/codes/c/chapter_computational_complexity/space_complexity.c @@ -30,12 +30,13 @@ void constant(int n) { } /* 哈希表 */ -typedef struct hashTable { +struct hashTable { int key; int val; - // 借助 LetCode 上常用的哈希表 - UT_hash_handle hh; -} hashTable; + UT_hash_handle hh; // 基于 uthash.h 实现 +}; + +typedef struct hashTable hashTable; /* 线性阶 */ void linear(int n) { @@ -56,7 +57,6 @@ void linear(int n) { // 长度为 n 的哈希表占用 O(n) 空间 hashTable *h = NULL; - for (int i = 0; i < n; i++) { hashTable *tmp = malloc(sizeof(hashTable)); tmp->key = i; diff --git a/codes/c/chapter_searching/hashing_search.c b/codes/c/chapter_searching/hashing_search.c index a47438ed3..24ad7b79b 100644 --- a/codes/c/chapter_searching/hashing_search.c +++ b/codes/c/chapter_searching/hashing_search.c @@ -8,9 +8,9 @@ /* 哈希表 */ struct hashTable { - int key; // key 为 int 型 - void *val; // val 为 void * 型 - UT_hash_handle hh; // 借助 uthash 实现的哈希表 + int key; + void *val; + UT_hash_handle hh; // 基于 uthash.h 实现 }; typedef struct hashTable hashTable; diff --git a/codes/c/chapter_searching/leetcode_two_sum.c b/codes/c/chapter_searching/leetcode_two_sum.c index 20a34eea7..969bee80d 100644 --- a/codes/c/chapter_searching/leetcode_two_sum.c +++ b/codes/c/chapter_searching/leetcode_two_sum.c @@ -26,8 +26,7 @@ int *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) { struct hashTable { int key; int val; - // 借助 LetCode 上常用的哈希表 - UT_hash_handle hh; + UT_hash_handle hh; // 基于 uthash.h 实现 }; typedef struct hashTable hashTable; diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index 9df8e3ee3..1bc53da92 100755 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -705,6 +705,8 @@ $$ === "C" ```c title="space_complexity.c" + [class]{hashTable}-[func]{} + [class]{}-[func]{linear} ``` diff --git a/docs/chapter_searching/replace_linear_by_hashing.md b/docs/chapter_searching/replace_linear_by_hashing.md index fcd87f311..462980f66 100755 --- a/docs/chapter_searching/replace_linear_by_hashing.md +++ b/docs/chapter_searching/replace_linear_by_hashing.md @@ -126,6 +126,8 @@ === "C" ```c title="leetcode_two_sum.c" + [class]{hashTable}-[func]{} + [class]{}-[func]{twoSumHashTable} ```