mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-14 12:40:58 +00:00
fix(lang: c, chapters: searching, sorting): Some serious errors (#1531)
* fix: correct hash table insertion * Use a pointer to a pointer instead of a single pointer in the insert function, ensuring proper updates to the hash table * Bug fixes: This fixes issues with empty results. * fix: correct issues in radix sort * Replaced loop-based array copy with memcpy to ensure efficient memory copying in countingSortDigit * Fixed a bug in radixSort where the maximum value search loop skipped the last element * Bug fix: Ensures all elements are checked when determining the maximum value in the array * perf: improve memory management by adding explicit memory release * revert: Revert to using the old loop override array method and drop specific api's like memcpy.
This commit is contained in:
@@ -37,12 +37,12 @@ HashTable *find(HashTable *h, int key) {
|
||||
}
|
||||
|
||||
/* 哈希表元素插入 */
|
||||
void insert(HashTable *h, int key, int val) {
|
||||
HashTable *t = find(h, key);
|
||||
void insert(HashTable **h, int key, int val) {
|
||||
HashTable *t = find(*h, key);
|
||||
if (t == NULL) {
|
||||
HashTable *tmp = malloc(sizeof(HashTable));
|
||||
tmp->key = key, tmp->val = val;
|
||||
HASH_ADD_INT(h, key, tmp);
|
||||
HASH_ADD_INT(*h, key, tmp);
|
||||
} else {
|
||||
t->val = val;
|
||||
}
|
||||
@@ -59,7 +59,7 @@ int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {
|
||||
*returnSize = 2;
|
||||
return res;
|
||||
}
|
||||
insert(hashtable, nums[i], i);
|
||||
insert(&hashtable, nums[i], i);
|
||||
}
|
||||
*returnSize = 0;
|
||||
return NULL;
|
||||
@@ -83,4 +83,4 @@ int main() {
|
||||
printArray(res, returnSize);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user