mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-22 00:07:13 +00:00
feat: Traditional Chinese version (#1163)
* First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology. * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * Update terminology.md * 操作数量(num. of operations)-> 操作數量 * 字首和->前綴和 * Update figures * 歸 -> 迴 記憶體洩漏 -> 記憶體流失 * Fix the bug of the file filter * 支援 -> 支持 Add zh-Hant/README.md * Add the zh-Hant chapter covers. Bug fixes. * 外掛 -> 擴充功能 * Add the landing page for zh-Hant version * Unify the font of the chapter covers for the zh, en, and zh-Hant version * Move zh-Hant/ to zh-hant/ * Translate terminology.md to traditional Chinese
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
// File: array.go
|
||||
// Created Time: 2022-12-29
|
||||
// Author: GuoWei (gongguowei01@gmail.com), cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
/* 隨機訪問元素 */
|
||||
func randomAccess(nums []int) (randomNum int) {
|
||||
// 在區間 [0, nums.length) 中隨機抽取一個數字
|
||||
randomIndex := rand.Intn(len(nums))
|
||||
// 獲取並返回隨機元素
|
||||
randomNum = nums[randomIndex]
|
||||
return
|
||||
}
|
||||
|
||||
/* 擴展陣列長度 */
|
||||
func extend(nums []int, enlarge int) []int {
|
||||
// 初始化一個擴展長度後的陣列
|
||||
res := make([]int, len(nums)+enlarge)
|
||||
// 將原陣列中的所有元素複製到新陣列
|
||||
for i, num := range nums {
|
||||
res[i] = num
|
||||
}
|
||||
// 返回擴展後的新陣列
|
||||
return res
|
||||
}
|
||||
|
||||
/* 在陣列的索引 index 處插入元素 num */
|
||||
func insert(nums []int, num int, index int) {
|
||||
// 把索引 index 以及之後的所有元素向後移動一位
|
||||
for i := len(nums) - 1; i > index; i-- {
|
||||
nums[i] = nums[i-1]
|
||||
}
|
||||
// 將 num 賦給 index 處的元素
|
||||
nums[index] = num
|
||||
}
|
||||
|
||||
/* 刪除索引 index 處的元素 */
|
||||
func remove(nums []int, index int) {
|
||||
// 把索引 index 之後的所有元素向前移動一位
|
||||
for i := index; i < len(nums)-1; i++ {
|
||||
nums[i] = nums[i+1]
|
||||
}
|
||||
}
|
||||
|
||||
/* 走訪陣列 */
|
||||
func traverse(nums []int) {
|
||||
count := 0
|
||||
// 透過索引走訪陣列
|
||||
for i := 0; i < len(nums); i++ {
|
||||
count += nums[i]
|
||||
}
|
||||
count = 0
|
||||
// 直接走訪陣列元素
|
||||
for _, num := range nums {
|
||||
count += num
|
||||
}
|
||||
// 同時走訪資料索引和元素
|
||||
for i, num := range nums {
|
||||
count += nums[i]
|
||||
count += num
|
||||
}
|
||||
}
|
||||
|
||||
/* 在陣列中查詢指定元素 */
|
||||
func find(nums []int, target int) (index int) {
|
||||
index = -1
|
||||
for i := 0; i < len(nums); i++ {
|
||||
if nums[i] == target {
|
||||
index = i
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// File: array_test.go
|
||||
// Created Time: 2022-12-29
|
||||
// Author: GuoWei (gongguowei01@gmail.com), cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
/**
|
||||
我們將 Go 中的 Slice 切片看作 Array 陣列。因為這樣可以
|
||||
降低理解成本,利於我們將關注點放在資料結構與演算法上。
|
||||
*/
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/* Driver Code */
|
||||
func TestArray(t *testing.T) {
|
||||
/* 初始化陣列 */
|
||||
var arr [5]int
|
||||
fmt.Println("陣列 arr =", arr)
|
||||
// 在 Go 中,指定長度時([5]int)為陣列,不指定長度時([]int)為切片
|
||||
// 由於 Go 的陣列被設計為在編譯期確定長度,因此只能使用常數來指定長度
|
||||
// 為了方便實現擴容 extend() 函式,以下將切片(Slice)看作陣列(Array)
|
||||
nums := []int{1, 3, 2, 5, 4}
|
||||
fmt.Println("陣列 nums =", nums)
|
||||
|
||||
/* 隨機訪問 */
|
||||
randomNum := randomAccess(nums)
|
||||
fmt.Println("在 nums 中獲取隨機元素", randomNum)
|
||||
|
||||
/* 長度擴展 */
|
||||
nums = extend(nums, 3)
|
||||
fmt.Println("將陣列長度擴展至 8 ,得到 nums =", nums)
|
||||
|
||||
/* 插入元素 */
|
||||
insert(nums, 6, 3)
|
||||
fmt.Println("在索引 3 處插入數字 6 ,得到 nums =", nums)
|
||||
|
||||
/* 刪除元素 */
|
||||
remove(nums, 2)
|
||||
fmt.Println("刪除索引 2 處的元素,得到 nums =", nums)
|
||||
|
||||
/* 走訪陣列 */
|
||||
traverse(nums)
|
||||
|
||||
/* 查詢元素 */
|
||||
index := find(nums, 3)
|
||||
fmt.Println("在 nums 中查詢元素 3 ,得到索引 =", index)
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// File: linked_list.go
|
||||
// Created Time: 2022-12-29
|
||||
// Author: cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
import (
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
/* 在鏈結串列的節點 n0 之後插入節點 P */
|
||||
func insertNode(n0 *ListNode, P *ListNode) {
|
||||
n1 := n0.Next
|
||||
P.Next = n1
|
||||
n0.Next = P
|
||||
}
|
||||
|
||||
/* 刪除鏈結串列的節點 n0 之後的首個節點 */
|
||||
func removeItem(n0 *ListNode) {
|
||||
if n0.Next == nil {
|
||||
return
|
||||
}
|
||||
// n0 -> P -> n1
|
||||
P := n0.Next
|
||||
n1 := P.Next
|
||||
n0.Next = n1
|
||||
}
|
||||
|
||||
/* 訪問鏈結串列中索引為 index 的節點 */
|
||||
func access(head *ListNode, index int) *ListNode {
|
||||
for i := 0; i < index; i++ {
|
||||
if head == nil {
|
||||
return nil
|
||||
}
|
||||
head = head.Next
|
||||
}
|
||||
return head
|
||||
}
|
||||
|
||||
/* 在鏈結串列中查詢值為 target 的首個節點 */
|
||||
func findNode(head *ListNode, target int) int {
|
||||
index := 0
|
||||
for head != nil {
|
||||
if head.Val == target {
|
||||
return index
|
||||
}
|
||||
head = head.Next
|
||||
index++
|
||||
}
|
||||
return -1
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// File: linked_list_test.go
|
||||
// Created Time: 2022-12-29
|
||||
// Author: cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestLinkedList(t *testing.T) {
|
||||
/* 初始化鏈結串列 1 -> 3 -> 2 -> 5 -> 4 */
|
||||
// 初始化各個節點
|
||||
n0 := NewListNode(1)
|
||||
n1 := NewListNode(3)
|
||||
n2 := NewListNode(2)
|
||||
n3 := NewListNode(5)
|
||||
n4 := NewListNode(4)
|
||||
|
||||
// 構建節點之間的引用
|
||||
n0.Next = n1
|
||||
n1.Next = n2
|
||||
n2.Next = n3
|
||||
n3.Next = n4
|
||||
fmt.Println("初始化的鏈結串列為")
|
||||
PrintLinkedList(n0)
|
||||
|
||||
/* 插入節點 */
|
||||
insertNode(n0, NewListNode(0))
|
||||
fmt.Println("插入節點後的鏈結串列為")
|
||||
PrintLinkedList(n0)
|
||||
|
||||
/* 刪除節點 */
|
||||
removeItem(n0)
|
||||
fmt.Println("刪除節點後的鏈結串列為")
|
||||
PrintLinkedList(n0)
|
||||
|
||||
/* 訪問節點 */
|
||||
node := access(n0, 3)
|
||||
fmt.Println("鏈結串列中索引 3 處的節點的值 =", node)
|
||||
|
||||
/* 查詢節點 */
|
||||
index := findNode(n0, 2)
|
||||
fmt.Println("鏈結串列中值為 2 的節點的索引 =", index)
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// File: list_test.go
|
||||
// Created Time: 2022-12-18
|
||||
// Author: msk397 (machangxinq@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/* Driver Code */
|
||||
func TestList(t *testing.T) {
|
||||
/* 初始化串列 */
|
||||
nums := []int{1, 3, 2, 5, 4}
|
||||
fmt.Println("串列 nums =", nums)
|
||||
|
||||
/* 訪問元素 */
|
||||
num := nums[1] // 訪問索引 1 處的元素
|
||||
fmt.Println("訪問索引 1 處的元素,得到 num =", num)
|
||||
|
||||
/* 更新元素 */
|
||||
nums[1] = 0 // 將索引 1 處的元素更新為 0
|
||||
fmt.Println("將索引 1 處的元素更新為 0 ,得到 nums =", nums)
|
||||
|
||||
/* 清空串列 */
|
||||
nums = nil
|
||||
fmt.Println("清空串列後 nums =", nums)
|
||||
|
||||
/* 在尾部新增元素 */
|
||||
nums = append(nums, 1)
|
||||
nums = append(nums, 3)
|
||||
nums = append(nums, 2)
|
||||
nums = append(nums, 5)
|
||||
nums = append(nums, 4)
|
||||
fmt.Println("新增元素後 nums =", nums)
|
||||
|
||||
/* 在中間插入元素 */
|
||||
nums = append(nums[:3], append([]int{6}, nums[3:]...)...) // 在索引 3 處插入數字 6
|
||||
fmt.Println("在索引 3 處插入數字 6 ,得到 nums =", nums)
|
||||
|
||||
/* 刪除元素 */
|
||||
nums = append(nums[:3], nums[4:]...) // 刪除索引 3 處的元素
|
||||
fmt.Println("刪除索引 3 處的元素,得到 nums =", nums)
|
||||
|
||||
/* 透過索引走訪串列 */
|
||||
count := 0
|
||||
for i := 0; i < len(nums); i++ {
|
||||
count += nums[i]
|
||||
}
|
||||
/* 直接走訪串列元素 */
|
||||
count = 0
|
||||
for _, x := range nums {
|
||||
count += x
|
||||
}
|
||||
|
||||
/* 拼接兩個串列 */
|
||||
nums1 := []int{6, 8, 7, 10, 9}
|
||||
nums = append(nums, nums1...) // 將串列 nums1 拼接到 nums 之後
|
||||
fmt.Println("將串列 nums1 拼接到 nums 之後,得到 nums =", nums)
|
||||
|
||||
/* 排序串列 */
|
||||
sort.Ints(nums) // 排序後,串列元素從小到大排列
|
||||
fmt.Println("排序串列後 nums =", nums)
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
// File: my_list.go
|
||||
// Created Time: 2022-12-18
|
||||
// Author: msk397 (machangxinq@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
/* 串列類別 */
|
||||
type myList struct {
|
||||
arrCapacity int
|
||||
arr []int
|
||||
arrSize int
|
||||
extendRatio int
|
||||
}
|
||||
|
||||
/* 建構子 */
|
||||
func newMyList() *myList {
|
||||
return &myList{
|
||||
arrCapacity: 10, // 串列容量
|
||||
arr: make([]int, 10), // 陣列(儲存串列元素)
|
||||
arrSize: 0, // 串列長度(當前元素數量)
|
||||
extendRatio: 2, // 每次串列擴容的倍數
|
||||
}
|
||||
}
|
||||
|
||||
/* 獲取串列長度(當前元素數量) */
|
||||
func (l *myList) size() int {
|
||||
return l.arrSize
|
||||
}
|
||||
|
||||
/* 獲取串列容量 */
|
||||
func (l *myList) capacity() int {
|
||||
return l.arrCapacity
|
||||
}
|
||||
|
||||
/* 訪問元素 */
|
||||
func (l *myList) get(index int) int {
|
||||
// 索引如果越界,則丟擲異常,下同
|
||||
if index < 0 || index >= l.arrSize {
|
||||
panic("索引越界")
|
||||
}
|
||||
return l.arr[index]
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
func (l *myList) set(num, index int) {
|
||||
if index < 0 || index >= l.arrSize {
|
||||
panic("索引越界")
|
||||
}
|
||||
l.arr[index] = num
|
||||
}
|
||||
|
||||
/* 在尾部新增元素 */
|
||||
func (l *myList) add(num int) {
|
||||
// 元素數量超出容量時,觸發擴容機制
|
||||
if l.arrSize == l.arrCapacity {
|
||||
l.extendCapacity()
|
||||
}
|
||||
l.arr[l.arrSize] = num
|
||||
// 更新元素數量
|
||||
l.arrSize++
|
||||
}
|
||||
|
||||
/* 在中間插入元素 */
|
||||
func (l *myList) insert(num, index int) {
|
||||
if index < 0 || index >= l.arrSize {
|
||||
panic("索引越界")
|
||||
}
|
||||
// 元素數量超出容量時,觸發擴容機制
|
||||
if l.arrSize == l.arrCapacity {
|
||||
l.extendCapacity()
|
||||
}
|
||||
// 將索引 index 以及之後的元素都向後移動一位
|
||||
for j := l.arrSize - 1; j >= index; j-- {
|
||||
l.arr[j+1] = l.arr[j]
|
||||
}
|
||||
l.arr[index] = num
|
||||
// 更新元素數量
|
||||
l.arrSize++
|
||||
}
|
||||
|
||||
/* 刪除元素 */
|
||||
func (l *myList) remove(index int) int {
|
||||
if index < 0 || index >= l.arrSize {
|
||||
panic("索引越界")
|
||||
}
|
||||
num := l.arr[index]
|
||||
// 將索引 index 之後的元素都向前移動一位
|
||||
for j := index; j < l.arrSize-1; j++ {
|
||||
l.arr[j] = l.arr[j+1]
|
||||
}
|
||||
// 更新元素數量
|
||||
l.arrSize--
|
||||
// 返回被刪除的元素
|
||||
return num
|
||||
}
|
||||
|
||||
/* 串列擴容 */
|
||||
func (l *myList) extendCapacity() {
|
||||
// 新建一個長度為原陣列 extendRatio 倍的新陣列,並將原陣列複製到新陣列
|
||||
l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)
|
||||
// 更新串列容量
|
||||
l.arrCapacity = len(l.arr)
|
||||
}
|
||||
|
||||
/* 返回有效長度的串列 */
|
||||
func (l *myList) toArray() []int {
|
||||
// 僅轉換有效長度範圍內的串列元素
|
||||
return l.arr[:l.arrSize]
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// File: my_list_test.go
|
||||
// Created Time: 2022-12-18
|
||||
// Author: msk397 (machangxinq@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/* Driver Code */
|
||||
func TestMyList(t *testing.T) {
|
||||
/* 初始化串列 */
|
||||
nums := newMyList()
|
||||
/* 在尾部新增元素 */
|
||||
nums.add(1)
|
||||
nums.add(3)
|
||||
nums.add(2)
|
||||
nums.add(5)
|
||||
nums.add(4)
|
||||
fmt.Printf("串列 nums = %v ,容量 = %v ,長度 = %v\n", nums.toArray(), nums.capacity(), nums.size())
|
||||
|
||||
/* 在中間插入元素 */
|
||||
nums.insert(6, 3)
|
||||
fmt.Printf("在索引 3 處插入數字 6 ,得到 nums = %v\n", nums.toArray())
|
||||
|
||||
/* 刪除元素 */
|
||||
nums.remove(3)
|
||||
fmt.Printf("刪除索引 3 處的元素,得到 nums = %v\n", nums.toArray())
|
||||
|
||||
/* 訪問元素 */
|
||||
num := nums.get(1)
|
||||
fmt.Printf("訪問索引 1 處的元素,得到 num = %v\n", num)
|
||||
|
||||
/* 更新元素 */
|
||||
nums.set(0, 1)
|
||||
fmt.Printf("將索引 1 處的元素更新為 0 ,得到 nums = %v\n", nums.toArray())
|
||||
|
||||
/* 測試擴容機制 */
|
||||
for i := 0; i < 10; i++ {
|
||||
// 在 i = 5 時,串列長度將超出串列容量,此時觸發擴容機制
|
||||
nums.add(i)
|
||||
}
|
||||
fmt.Printf("擴容後的串列 nums = %v ,容量 = %v ,長度 = %v\n", nums.toArray(), nums.capacity(), nums.size())
|
||||
}
|
||||
Reference in New Issue
Block a user