Skip to content

Commit

Permalink
Bug fixes and improvements (#1572)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
krahets authored Nov 25, 2024
1 parent 01a5f7b commit 2a9db6d
Show file tree
Hide file tree
Showing 65 changed files with 414 additions and 140 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,4 @@ <h3>贡献者</h3>
</a>
</div>
</div>
</section>
</section>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion en/docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,4 @@ <h3>Contributors</h3>
</a>
</div>
</div>
</section>
</section>
10 changes: 5 additions & 5 deletions zh-hant/codes/c/chapter_searching/two_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -83,4 +83,4 @@ int main() {
printArray(res, returnSize);

return 0;
}
}
1 change: 1 addition & 0 deletions zh-hant/codes/c/chapter_sorting/counting_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void countingSort(int nums[], int size) {
// 使用結果陣列 res 覆蓋原陣列 nums
memcpy(nums, res, size * sizeof(int));
// 5. 釋放記憶體
free(res);
free(counter);
}

Expand Down
6 changes: 5 additions & 1 deletion zh-hant/codes/c/chapter_sorting/radix_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void Test() {

// 暴力搜尋
int res = MinPathSumDFS(grid, n - 1, m - 1);
Console.WriteLine("從左上角到右下角的做小路徑和為 " + res);
Console.WriteLine("從左上角到右下角的最小路徑和為 " + res);

// 記憶化搜尋
int[][] mem = new int[n][];
Expand All @@ -114,14 +114,14 @@ public void Test() {
Array.Fill(mem[i], -1);
}
res = MinPathSumDFSMem(grid, mem, n - 1, m - 1);
Console.WriteLine("從左上角到右下角的做小路徑和為 " + res);
Console.WriteLine("從左上角到右下角的最小路徑和為 " + res);

// 動態規劃
res = MinPathSumDP(grid);
Console.WriteLine("從左上角到右下角的做小路徑和為 " + res);
Console.WriteLine("從左上角到右下角的最小路徑和為 " + res);

// 空間最佳化後的動態規劃
res = MinPathSumDPComp(grid);
Console.WriteLine("從左上角到右下角的做小路徑和為 " + res);
Console.WriteLine("從左上角到右下角的最小路徑和為 " + res);
}
}
4 changes: 2 additions & 2 deletions zh-hant/codes/csharp/chapter_heap/heap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public void Test() {
/* 初始化堆積 */
// 初始化小頂堆積
PriorityQueue<int, int> minHeap = new();
// 初始化大頂堆積(使用 lambda 表示式修改 Comparator 即可)
PriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));
// 初始化大頂堆積(使用 lambda 表示式修改 Comparer 即可)
PriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y.CompareTo(x)));
Console.WriteLine("以下測試樣例為大頂堆積");

/* 元素入堆積 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,18 @@ void main() {

// 暴力搜尋
int res = minPathSumDFS(grid, n - 1, m - 1);
print("從左上角到右下角的做小路徑和為 $res");
print("從左上角到右下角的最小路徑和為 $res");

// 記憶化搜尋
List<List<int>> mem = List.generate(n, (i) => List.filled(m, -1));
res = minPathSumDFSMem(grid, mem, n - 1, m - 1);
print("從左上角到右下角的做小路徑和為 $res");
print("從左上角到右下角的最小路徑和為 $res");

// 動態規劃
res = minPathSumDP(grid);
print("從左上角到右下角的做小路徑和為 $res");
print("從左上角到右下角的最小路徑和為 $res");

// 空間最佳化後的動態規劃
res = minPathSumDPComp(grid);
print("從左上角到右下角的做小路徑和為 $res");
print("從左上角到右下角的最小路徑和為 $res");
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestMinPathSum(t *testing.T) {

// 暴力搜尋
res := minPathSumDFS(grid, n-1, m-1)
fmt.Printf("從左上角到右下角的做小路徑和為 %d\n", res)
fmt.Printf("從左上角到右下角的最小路徑和為 %d\n", res)

// 記憶化搜尋
mem := make([][]int, n)
Expand All @@ -31,13 +31,13 @@ func TestMinPathSum(t *testing.T) {
}
}
res = minPathSumDFSMem(grid, mem, n-1, m-1)
fmt.Printf("從左上角到右下角的做小路徑和為 %d\n", res)
fmt.Printf("從左上角到右下角的最小路徑和為 %d\n", res)

// 動態規劃
res = minPathSumDP(grid)
fmt.Printf("從左上角到右下角的做小路徑和為 %d\n", res)
fmt.Printf("從左上角到右下角的最小路徑和為 %d\n", res)

// 空間最佳化後的動態規劃
res = minPathSumDPComp(grid)
fmt.Printf("從左上角到右下角的做小路徑和為 %d\n", res)
fmt.Printf("從左上角到右下角的最小路徑和為 %d\n", res)
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ def min_path_sum_dp_comp(grid: list[list[int]]) -> int:

# 暴力搜尋
res = min_path_sum_dfs(grid, n - 1, m - 1)
print(f"從左上角到右下角的做小路徑和為 {res}")
print(f"從左上角到右下角的最小路徑和為 {res}")

# 記憶化搜尋
mem = [[-1] * m for _ in range(n)]
res = min_path_sum_dfs_mem(grid, mem, n - 1, m - 1)
print(f"從左上角到右下角的做小路徑和為 {res}")
print(f"從左上角到右下角的最小路徑和為 {res}")

# 動態規劃
res = min_path_sum_dp(grid)
print(f"從左上角到右下角的做小路徑和為 {res}")
print(f"從左上角到右下角的最小路徑和為 {res}")

# 空間最佳化後的動態規劃
res = min_path_sum_dp_comp(grid)
print(f"從左上角到右下角的做小路徑和為 {res}")
print(f"從左上角到右下角的最小路徑和為 {res}")
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ def min_path_sum_dp_comp(grid)

# 暴力搜尋
res = min_path_sum_dfs(grid, n - 1, m - 1)
puts "從左上角到右下角的做小路徑和為 #{res}"
puts "從左上角到右下角的最小路徑和為 #{res}"

# 記憶化搜尋
mem = Array.new(n) { Array.new(m, - 1) }
res = min_path_sum_dfs_mem(grid, mem, n - 1, m -1)
puts "從左上角到右下角的做小路徑和為 #{res}"
puts "從左上角到右下角的最小路徑和為 #{res}"

# 動態規劃
res = min_path_sum_dp(grid)
puts "從左上角到右下角的做小路徑和為 #{res}"
puts "從左上角到右下角的最小路徑和為 #{res}"

# 空間最佳化後的動態規劃
res = min_path_sum_dp_comp(grid)
puts "從左上角到右下角的做小路徑和為 #{res}"
puts "從左上角到右下角的最小路徑和為 #{res}"
end
3 changes: 1 addition & 2 deletions zh-hant/codes/rust/chapter_array_and_linkedlist/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* Author: xBLACICEx ([email protected]), codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::print_util;
use rand::Rng;

/* 隨機訪問元素 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use list_node::ListNode;
use hello_algo_rust::include::{print_util, ListNode};
use std::cell::RefCell;
use std::rc::Rc;

Expand Down
3 changes: 1 addition & 2 deletions zh-hant/codes/rust/chapter_array_and_linkedlist/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* Created Time: 2023-01-18
* Author: xBLACICEx ([email protected]), codingonion ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* Driver Code */
fn main() {
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/rust/chapter_array_and_linkedlist/my_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* 串列類別 */
#[allow(dead_code)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode};
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

/* 前序走訪:例題一 */
fn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<&Rc<RefCell<TreeNode>>>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode};
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

/* 前序走訪:例題二 */
fn pre_order(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode};
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

/* 前序走訪:例題三 */
fn pre_order(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode};
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};

/* 判斷當前狀態是否為解 */
fn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");

use list_node::ListNode;
use hello_algo_rust::include::{print_util, ListNode, TreeNode};
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use tree_node::TreeNode;

/* 函式 */
fn function() -> i32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* Author: xBLACICEx ([email protected]), codingonion ([email protected])
*/

include!("../include/include.rs");

use hello_algo_rust::include::print_util;
use rand::seq::SliceRandom;
use rand::thread_rng;

Expand Down
3 changes: 1 addition & 2 deletions zh-hant/codes/rust/chapter_divide_and_conquer/build_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
* Author: codingonion ([email protected])
*/

use hello_algo_rust::include::{print_util, TreeNode};
use std::collections::HashMap;
use std::{cell::RefCell, rc::Rc};
include!("../include/include.rs");
use tree_node::TreeNode;

/* 構建二元樹:分治 */
fn dfs(
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/rust/chapter_graph/graph_adjacency_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: night-cruise ([email protected])
*/

include!("../include/vertex.rs");
pub use hello_algo_rust::include::{vals_to_vets, vets_to_vals, Vertex};

use std::collections::HashMap;

Expand Down
3 changes: 1 addition & 2 deletions zh-hant/codes/rust/chapter_hashing/build_in_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
* Author: WSL0809 ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::ListNode;

use crate::list_node::ListNode;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/rust/chapter_hashing/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: codingonion ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/rust/chapter_heap/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: night-cruise ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

use std::collections::BinaryHeap;

Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/rust/chapter_heap/my_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: night-cruise ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

/* 大頂堆積 */
struct MaxHeap {
Expand Down
2 changes: 1 addition & 1 deletion zh-hant/codes/rust/chapter_heap/top_k.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: night-cruise ([email protected])
*/

include!("../include/include.rs");
use hello_algo_rust::include::print_util;

use std::cmp::Reverse;
use std::collections::BinaryHeap;
Expand Down
Loading

0 comments on commit 2a9db6d

Please sign in to comment.