Bug fixes and improvements (#1572)

* Sync zh and zh-hant versions.

* Remove the polyfill.io link from mkdocs.yml

* Update contributors' info for code reviewers and en/zh-hant versions reviewers.

* Fix graph.md

* Update avatars for English version reviewers.

* Sync zh and zh-hant versions.

* Fix two_sum_brute_force.png

* Sync zh and zh-hant versions.
Optimize structrue of index.html.

* Format index.html
This commit is contained in:
Yudong Jin
2024-11-25 19:21:11 +08:00
committed by GitHub
parent 01a5f7b09f
commit 2a9db6d039
65 changed files with 414 additions and 140 deletions

View File

@ -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;
}
}

View File

@ -65,6 +65,7 @@ void countingSort(int nums[], int size) {
// 使用結果陣列 res 覆蓋原陣列 nums
memcpy(nums, res, size * sizeof(int));
// 5. 釋放記憶體
free(res);
free(counter);
}

View File

@ -16,6 +16,7 @@ int digit(int num, int exp) {
void countingSortDigit(int nums[], int size, int exp) {
// 十進位制的位範圍為 0~9 ,因此需要長度為 10 的桶陣列
int *counter = (int *)malloc((sizeof(int) * 10));
memset(counter, 0, sizeof(int) * 10); // 初始化為 0 以支持後續記憶體釋放
// 統計 0~9 各數字的出現次數
for (int i = 0; i < size; i++) {
// 獲取 nums[i] 第 k 位,記為 d
@ -39,13 +40,16 @@ void countingSortDigit(int nums[], int size, int exp) {
for (int i = 0; i < size; i++) {
nums[i] = res[i];
}
// 釋放記憶體
free(res);
free(counter);
}
/* 基數排序 */
void radixSort(int nums[], int size) {
// 獲取陣列的最大元素,用於判斷最大位數
int max = INT32_MIN;
for (size_t i = 0; i < size - 1; i++) {
for (int i = 0; i < size; i++) {
if (nums[i] > max) {
max = nums[i];
}