From db64108e5dea7dca4c745ed4fb700cab4dd22d8b Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 12 Jun 2025 20:45:41 +0800 Subject: [PATCH] Bug fixes (#1766) * Sync zh and zh-hant versions. * Bug fixes. --- .../space_complexity.md | 8 ++++---- .../space_complexity.md | 8 ++++---- .../javascript/chapter_sorting/counting_sort.js | 10 ++-------- .../codes/javascript/chapter_sorting/radix_sort.js | 7 +------ .../typescript/chapter_sorting/counting_sort.ts | 10 ++-------- .../codes/typescript/chapter_sorting/radix_sort.ts | 7 +------ .../space_complexity.md | 8 ++++---- .../docs/chapter_data_structure/basic_data_types.md | 12 ++++++++++++ zh-hant/docs/chapter_searching/binary_search.md | 2 +- 9 files changed, 31 insertions(+), 41 deletions(-) diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index e6909ae8d..82b759759 100755 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -572,7 +572,7 @@ /* 递归的空间复杂度为 O(n) */ void recur(int n) { if (n == 1) return; - return recur(n - 1); + recur(n - 1); } ``` @@ -592,7 +592,7 @@ /* 递归的空间复杂度为 O(n) */ void recur(int n) { if (n == 1) return; - return recur(n - 1); + recur(n - 1); } ``` @@ -721,7 +721,7 @@ /* 递归的空间复杂度为 O(n) */ void recur(int n) { if (n == 1) return; - return recur(n - 1); + recur(n - 1); } ``` @@ -763,7 +763,7 @@ /* 递归的空间复杂度为 O(n) */ void recur(int n) { if (n == 1) return; - return recur(n - 1); + recur(n - 1); } ``` diff --git a/en/docs/chapter_computational_complexity/space_complexity.md b/en/docs/chapter_computational_complexity/space_complexity.md index 20157220e..33b16f089 100644 --- a/en/docs/chapter_computational_complexity/space_complexity.md +++ b/en/docs/chapter_computational_complexity/space_complexity.md @@ -511,7 +511,7 @@ Consider the following code, the term "worst-case" in worst-case space complexit /* Recursion O(n) */ void recur(int n) { if (n == 1) return; - return recur(n - 1); + recur(n - 1); } ``` @@ -531,7 +531,7 @@ Consider the following code, the term "worst-case" in worst-case space complexit /* Recursion O(n) */ void recur(int n) { if (n == 1) return; - return recur(n - 1); + recur(n - 1); } ``` @@ -660,7 +660,7 @@ Consider the following code, the term "worst-case" in worst-case space complexit /* Recursion O(n) */ void recur(int n) { if (n == 1) return; - return recur(n - 1); + recur(n - 1); } ``` @@ -702,7 +702,7 @@ Consider the following code, the term "worst-case" in worst-case space complexit /* Recursion O(n) */ void recur(int n) { if (n == 1) return; - return recur(n - 1); + recur(n - 1); } ``` diff --git a/zh-hant/codes/javascript/chapter_sorting/counting_sort.js b/zh-hant/codes/javascript/chapter_sorting/counting_sort.js index bb57a3f07..7663b1317 100644 --- a/zh-hant/codes/javascript/chapter_sorting/counting_sort.js +++ b/zh-hant/codes/javascript/chapter_sorting/counting_sort.js @@ -8,10 +8,7 @@ // 簡單實現,無法用於排序物件 function countingSortNaive(nums) { // 1. 統計陣列最大元素 m - let m = 0; - for (const num of nums) { - m = Math.max(m, num); - } + let m = Math.max(...nums); // 2. 統計各數字的出現次數 // counter[num] 代表 num 的出現次數 const counter = new Array(m + 1).fill(0); @@ -31,10 +28,7 @@ function countingSortNaive(nums) { // 完整實現,可排序物件,並且是穩定排序 function countingSort(nums) { // 1. 統計陣列最大元素 m - let m = 0; - for (const num of nums) { - m = Math.max(m, num); - } + let m = Math.max(...nums); // 2. 統計各數字的出現次數 // counter[num] 代表 num 的出現次數 const counter = new Array(m + 1).fill(0); diff --git a/zh-hant/codes/javascript/chapter_sorting/radix_sort.js b/zh-hant/codes/javascript/chapter_sorting/radix_sort.js index b4412d4a4..3300f8763 100644 --- a/zh-hant/codes/javascript/chapter_sorting/radix_sort.js +++ b/zh-hant/codes/javascript/chapter_sorting/radix_sort.js @@ -41,12 +41,7 @@ function countingSortDigit(nums, exp) { /* 基數排序 */ function radixSort(nums) { // 獲取陣列的最大元素,用於判斷最大位數 - let m = Number.MIN_VALUE; - for (const num of nums) { - if (num > m) { - m = num; - } - } + let m = Math.max(... nums); // 按照從低位到高位的順序走訪 for (let exp = 1; exp <= m; exp *= 10) { // 對陣列元素的第 k 位執行計數排序 diff --git a/zh-hant/codes/typescript/chapter_sorting/counting_sort.ts b/zh-hant/codes/typescript/chapter_sorting/counting_sort.ts index db62710be..c73ee71fd 100644 --- a/zh-hant/codes/typescript/chapter_sorting/counting_sort.ts +++ b/zh-hant/codes/typescript/chapter_sorting/counting_sort.ts @@ -8,10 +8,7 @@ // 簡單實現,無法用於排序物件 function countingSortNaive(nums: number[]): void { // 1. 統計陣列最大元素 m - let m = 0; - for (const num of nums) { - m = Math.max(m, num); - } + let m: number = Math.max(...nums); // 2. 統計各數字的出現次數 // counter[num] 代表 num 的出現次數 const counter: number[] = new Array(m + 1).fill(0); @@ -31,10 +28,7 @@ function countingSortNaive(nums: number[]): void { // 完整實現,可排序物件,並且是穩定排序 function countingSort(nums: number[]): void { // 1. 統計陣列最大元素 m - let m = 0; - for (const num of nums) { - m = Math.max(m, num); - } + let m: number = Math.max(...nums); // 2. 統計各數字的出現次數 // counter[num] 代表 num 的出現次數 const counter: number[] = new Array(m + 1).fill(0); diff --git a/zh-hant/codes/typescript/chapter_sorting/radix_sort.ts b/zh-hant/codes/typescript/chapter_sorting/radix_sort.ts index 91ca29dd2..4616de2f3 100644 --- a/zh-hant/codes/typescript/chapter_sorting/radix_sort.ts +++ b/zh-hant/codes/typescript/chapter_sorting/radix_sort.ts @@ -41,12 +41,7 @@ function countingSortDigit(nums: number[], exp: number): void { /* 基數排序 */ function radixSort(nums: number[]): void { // 獲取陣列的最大元素,用於判斷最大位數 - let m = Number.MIN_VALUE; - for (const num of nums) { - if (num > m) { - m = num; - } - } + let m: number = Math.max(... nums); // 按照從低位到高位的順序走訪 for (let exp = 1; exp <= m; exp *= 10) { // 對陣列元素的第 k 位執行計數排序 diff --git a/zh-hant/docs/chapter_computational_complexity/space_complexity.md b/zh-hant/docs/chapter_computational_complexity/space_complexity.md index 0a56ff7aa..77d433992 100755 --- a/zh-hant/docs/chapter_computational_complexity/space_complexity.md +++ b/zh-hant/docs/chapter_computational_complexity/space_complexity.md @@ -572,7 +572,7 @@ /* 遞迴的空間複雜度為 O(n) */ void recur(int n) { if (n == 1) return; - return recur(n - 1); + recur(n - 1); } ``` @@ -592,7 +592,7 @@ /* 遞迴的空間複雜度為 O(n) */ void recur(int n) { if (n == 1) return; - return recur(n - 1); + recur(n - 1); } ``` @@ -721,7 +721,7 @@ /* 遞迴的空間複雜度為 O(n) */ void recur(int n) { if (n == 1) return; - return recur(n - 1); + recur(n - 1); } ``` @@ -763,7 +763,7 @@ /* 遞迴的空間複雜度為 O(n) */ void recur(int n) { if (n == 1) return; - return recur(n - 1); + recur(n - 1); } ``` diff --git a/zh-hant/docs/chapter_data_structure/basic_data_types.md b/zh-hant/docs/chapter_data_structure/basic_data_types.md index dea80ea1f..de3608641 100644 --- a/zh-hant/docs/chapter_data_structure/basic_data_types.md +++ b/zh-hant/docs/chapter_data_structure/basic_data_types.md @@ -173,7 +173,19 @@ === "Zig" ```zig title="" + const hello = [5]u8{ 'h', 'e', 'l', 'l', 'o' }; + // 以上程式碼展示了定義一個字面量陣列的方式,其中你可以選擇指明陣列的大小或者使用 _ 代替。使用 _ 時,Zig 會嘗試自動計算陣列的長度 + + const matrix_4x4 = [4][4]f32{ + [_]f32{ 1.0, 0.0, 0.0, 0.0 }, + [_]f32{ 0.0, 1.0, 0.0, 1.0 }, + [_]f32{ 0.0, 0.0, 1.0, 0.0 }, + [_]f32{ 0.0, 0.0, 0.0, 1.0 }, + }; + // 多維陣列(矩陣)實際上就是巢狀陣列,我們很容易就可以建立一個多維陣列出來 + const array = [_:0]u8{ 1, 2, 3, 4 }; + // 定義一個哨兵終止陣列,本質上來說,這是為了相容 C 中的規定的字串結尾字元\0。我們使用語法 [N:x]T 來描述一個元素為型別 T,長度為 N 的陣列,在它對應 N 的索引處的值應該是 x ``` ??? pythontutor "視覺化執行" diff --git a/zh-hant/docs/chapter_searching/binary_search.md b/zh-hant/docs/chapter_searching/binary_search.md index 2aa549924..652030212 100755 --- a/zh-hant/docs/chapter_searching/binary_search.md +++ b/zh-hant/docs/chapter_searching/binary_search.md @@ -80,4 +80,4 @@ - 二分搜尋僅適用於有序資料。若輸入資料無序,為了使用二分搜尋而專門進行排序,得不償失。因為排序演算法的時間複雜度通常為 $O(n \log n)$ ,比線性查詢和二分搜尋都更高。對於頻繁插入元素的場景,為保持陣列有序性,需要將元素插入到特定位置,時間複雜度為 $O(n)$ ,也是非常昂貴的。 - 二分搜尋僅適用於陣列。二分搜尋需要跳躍式(非連續地)訪問元素,而在鏈結串列中執行跳躍式訪問的效率較低,因此不適合應用在鏈結串列或基於鏈結串列實現的資料結構。 -- 小資料量下,線性查詢效能更佳。線性查詢中,每輪只需 1 次判斷操作;而在二分搜尋中,需要 1 次加法、1 次除法、1 ~ 3 次判斷操作、1 次加法(減法),共 4 ~ 6 個單元操作;因此,當資料量 $n$ 較小時,線性查詢反而比二分搜尋更快。 +- 小資料量下,線性查詢效能更佳。在線性查詢中,每輪只需 1 次判斷操作;而在二分搜尋中,需要 1 次加法、1 次除法、1 ~ 3 次判斷操作、1 次加法(減法),共 4 ~ 6 個單元操作;因此,當資料量 $n$ 較小時,線性查詢反而比二分搜尋更快。