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,107 @@
|
||||
/**
|
||||
* File: array.swift
|
||||
* Created Time: 2023-01-05
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* 隨機訪問元素 */
|
||||
func randomAccess(nums: [Int]) -> Int {
|
||||
// 在區間 [0, nums.count) 中隨機抽取一個數字
|
||||
let randomIndex = nums.indices.randomElement()!
|
||||
// 獲取並返回隨機元素
|
||||
let randomNum = nums[randomIndex]
|
||||
return randomNum
|
||||
}
|
||||
|
||||
/* 擴展陣列長度 */
|
||||
func extend(nums: [Int], enlarge: Int) -> [Int] {
|
||||
// 初始化一個擴展長度後的陣列
|
||||
var res = Array(repeating: 0, count: nums.count + enlarge)
|
||||
// 將原陣列中的所有元素複製到新陣列
|
||||
for i in nums.indices {
|
||||
res[i] = nums[i]
|
||||
}
|
||||
// 返回擴展後的新陣列
|
||||
return res
|
||||
}
|
||||
|
||||
/* 在陣列的索引 index 處插入元素 num */
|
||||
func insert(nums: inout [Int], num: Int, index: Int) {
|
||||
// 把索引 index 以及之後的所有元素向後移動一位
|
||||
for i in nums.indices.dropFirst(index).reversed() {
|
||||
nums[i] = nums[i - 1]
|
||||
}
|
||||
// 將 num 賦給 index 處的元素
|
||||
nums[index] = num
|
||||
}
|
||||
|
||||
/* 刪除索引 index 處的元素 */
|
||||
func remove(nums: inout [Int], index: Int) {
|
||||
// 把索引 index 之後的所有元素向前移動一位
|
||||
for i in nums.indices.dropFirst(index).dropLast() {
|
||||
nums[i] = nums[i + 1]
|
||||
}
|
||||
}
|
||||
|
||||
/* 走訪陣列 */
|
||||
func traverse(nums: [Int]) {
|
||||
var count = 0
|
||||
// 透過索引走訪陣列
|
||||
for i in nums.indices {
|
||||
count += nums[i]
|
||||
}
|
||||
// 直接走訪陣列元素
|
||||
for num in nums {
|
||||
count += num
|
||||
}
|
||||
// 同時走訪資料索引和元素
|
||||
for (i, num) in nums.enumerated() {
|
||||
count += nums[i]
|
||||
count += num
|
||||
}
|
||||
}
|
||||
|
||||
/* 在陣列中查詢指定元素 */
|
||||
func find(nums: [Int], target: Int) -> Int {
|
||||
for i in nums.indices {
|
||||
if nums[i] == target {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
@main
|
||||
enum _Array {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* 初始化陣列 */
|
||||
let arr = Array(repeating: 0, count: 5)
|
||||
print("陣列 arr = \(arr)")
|
||||
var nums = [1, 3, 2, 5, 4]
|
||||
print("陣列 nums = \(nums)")
|
||||
|
||||
/* 隨機訪問 */
|
||||
let randomNum = randomAccess(nums: nums)
|
||||
print("在 nums 中獲取隨機元素 \(randomNum)")
|
||||
|
||||
/* 長度擴展 */
|
||||
nums = extend(nums: nums, enlarge: 3)
|
||||
print("將陣列長度擴展至 8 ,得到 nums = \(nums)")
|
||||
|
||||
/* 插入元素 */
|
||||
insert(nums: &nums, num: 6, index: 3)
|
||||
print("在索引 3 處插入數字 6 ,得到 nums = \(nums)")
|
||||
|
||||
/* 刪除元素 */
|
||||
remove(nums: &nums, index: 2)
|
||||
print("刪除索引 2 處的元素,得到 nums = \(nums)")
|
||||
|
||||
/* 走訪陣列 */
|
||||
traverse(nums: nums)
|
||||
|
||||
/* 查詢元素 */
|
||||
let index = find(nums: nums, target: 3)
|
||||
print("在 nums 中查詢元素 3 ,得到索引 = \(index)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* File: linked_list.swift
|
||||
* Created Time: 2023-01-08
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
/* 在鏈結串列的節點 n0 之後插入節點 P */
|
||||
func insert(n0: ListNode, P: ListNode) {
|
||||
let n1 = n0.next
|
||||
P.next = n1
|
||||
n0.next = P
|
||||
}
|
||||
|
||||
/* 刪除鏈結串列的節點 n0 之後的首個節點 */
|
||||
func remove(n0: ListNode) {
|
||||
if n0.next == nil {
|
||||
return
|
||||
}
|
||||
// n0 -> P -> n1
|
||||
let P = n0.next
|
||||
let n1 = P?.next
|
||||
n0.next = n1
|
||||
}
|
||||
|
||||
/* 訪問鏈結串列中索引為 index 的節點 */
|
||||
func access(head: ListNode, index: Int) -> ListNode? {
|
||||
var head: ListNode? = head
|
||||
for _ in 0 ..< index {
|
||||
if head == nil {
|
||||
return nil
|
||||
}
|
||||
head = head?.next
|
||||
}
|
||||
return head
|
||||
}
|
||||
|
||||
/* 在鏈結串列中查詢值為 target 的首個節點 */
|
||||
func find(head: ListNode, target: Int) -> Int {
|
||||
var head: ListNode? = head
|
||||
var index = 0
|
||||
while head != nil {
|
||||
if head?.val == target {
|
||||
return index
|
||||
}
|
||||
head = head?.next
|
||||
index += 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
@main
|
||||
enum LinkedList {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* 初始化鏈結串列 */
|
||||
// 初始化各個節點
|
||||
let n0 = ListNode(x: 1)
|
||||
let n1 = ListNode(x: 3)
|
||||
let n2 = ListNode(x: 2)
|
||||
let n3 = ListNode(x: 5)
|
||||
let n4 = ListNode(x: 4)
|
||||
// 構建節點之間的引用
|
||||
n0.next = n1
|
||||
n1.next = n2
|
||||
n2.next = n3
|
||||
n3.next = n4
|
||||
print("初始化的鏈結串列為")
|
||||
PrintUtil.printLinkedList(head: n0)
|
||||
|
||||
/* 插入節點 */
|
||||
insert(n0: n0, P: ListNode(x: 0))
|
||||
print("插入節點後的鏈結串列為")
|
||||
PrintUtil.printLinkedList(head: n0)
|
||||
|
||||
/* 刪除節點 */
|
||||
remove(n0: n0)
|
||||
print("刪除節點後的鏈結串列為")
|
||||
PrintUtil.printLinkedList(head: n0)
|
||||
|
||||
/* 訪問節點 */
|
||||
let node = access(head: n0, index: 3)
|
||||
print("鏈結串列中索引 3 處的節點的值 = \(node!.val)")
|
||||
|
||||
/* 查詢節點 */
|
||||
let index = find(head: n0, target: 2)
|
||||
print("鏈結串列中值為 2 的節點的索引 = \(index)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* File: list.swift
|
||||
* Created Time: 2023-01-08
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
@main
|
||||
enum List {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* 初始化串列 */
|
||||
var nums = [1, 3, 2, 5, 4]
|
||||
print("串列 nums = \(nums)")
|
||||
|
||||
/* 訪問元素 */
|
||||
let num = nums[1]
|
||||
print("訪問索引 1 處的元素,得到 num = \(num)")
|
||||
|
||||
/* 更新元素 */
|
||||
nums[1] = 0
|
||||
print("將索引 1 處的元素更新為 0 ,得到 nums = \(nums)")
|
||||
|
||||
/* 清空串列 */
|
||||
nums.removeAll()
|
||||
print("清空串列後 nums = \(nums)")
|
||||
|
||||
/* 在尾部新增元素 */
|
||||
nums.append(1)
|
||||
nums.append(3)
|
||||
nums.append(2)
|
||||
nums.append(5)
|
||||
nums.append(4)
|
||||
print("新增元素後 nums = \(nums)")
|
||||
|
||||
/* 在中間插入元素 */
|
||||
nums.insert(6, at: 3)
|
||||
print("在索引 3 處插入數字 6 ,得到 nums = \(nums)")
|
||||
|
||||
/* 刪除元素 */
|
||||
nums.remove(at: 3)
|
||||
print("刪除索引 3 處的元素,得到 nums = \(nums)")
|
||||
|
||||
/* 透過索引走訪串列 */
|
||||
var count = 0
|
||||
for i in nums.indices {
|
||||
count += nums[i]
|
||||
}
|
||||
/* 直接走訪串列元素 */
|
||||
count = 0
|
||||
for x in nums {
|
||||
count += x
|
||||
}
|
||||
|
||||
/* 拼接兩個串列 */
|
||||
let nums1 = [6, 8, 7, 10, 9]
|
||||
nums.append(contentsOf: nums1)
|
||||
print("將串列 nums1 拼接到 nums 之後,得到 nums = \(nums)")
|
||||
|
||||
/* 排序串列 */
|
||||
nums.sort()
|
||||
print("排序串列後 nums = \(nums)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* File: my_list.swift
|
||||
* Created Time: 2023-01-08
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* 串列類別 */
|
||||
class MyList {
|
||||
private var arr: [Int] // 陣列(儲存串列元素)
|
||||
private var _capacity: Int // 串列容量
|
||||
private var _size: Int // 串列長度(當前元素數量)
|
||||
private let extendRatio: Int // 每次串列擴容的倍數
|
||||
|
||||
/* 建構子 */
|
||||
init() {
|
||||
_capacity = 10
|
||||
_size = 0
|
||||
extendRatio = 2
|
||||
arr = Array(repeating: 0, count: _capacity)
|
||||
}
|
||||
|
||||
/* 獲取串列長度(當前元素數量)*/
|
||||
func size() -> Int {
|
||||
_size
|
||||
}
|
||||
|
||||
/* 獲取串列容量 */
|
||||
func capacity() -> Int {
|
||||
_capacity
|
||||
}
|
||||
|
||||
/* 訪問元素 */
|
||||
func get(index: Int) -> Int {
|
||||
// 索引如果越界則丟擲錯誤,下同
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
return arr[index]
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
func set(index: Int, num: Int) {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
arr[index] = num
|
||||
}
|
||||
|
||||
/* 在尾部新增元素 */
|
||||
func add(num: Int) {
|
||||
// 元素數量超出容量時,觸發擴容機制
|
||||
if size() == capacity() {
|
||||
extendCapacity()
|
||||
}
|
||||
arr[size()] = num
|
||||
// 更新元素數量
|
||||
_size += 1
|
||||
}
|
||||
|
||||
/* 在中間插入元素 */
|
||||
func insert(index: Int, num: Int) {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
// 元素數量超出容量時,觸發擴容機制
|
||||
if size() == capacity() {
|
||||
extendCapacity()
|
||||
}
|
||||
// 將索引 index 以及之後的元素都向後移動一位
|
||||
for j in (index ..< size()).reversed() {
|
||||
arr[j + 1] = arr[j]
|
||||
}
|
||||
arr[index] = num
|
||||
// 更新元素數量
|
||||
_size += 1
|
||||
}
|
||||
|
||||
/* 刪除元素 */
|
||||
@discardableResult
|
||||
func remove(index: Int) -> Int {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("索引越界")
|
||||
}
|
||||
let num = arr[index]
|
||||
// 將將索引 index 之後的元素都向前移動一位
|
||||
for j in index ..< (size() - 1) {
|
||||
arr[j] = arr[j + 1]
|
||||
}
|
||||
// 更新元素數量
|
||||
_size -= 1
|
||||
// 返回被刪除的元素
|
||||
return num
|
||||
}
|
||||
|
||||
/* 串列擴容 */
|
||||
func extendCapacity() {
|
||||
// 新建一個長度為原陣列 extendRatio 倍的新陣列,並將原陣列複製到新陣列
|
||||
arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))
|
||||
// 更新串列容量
|
||||
_capacity = arr.count
|
||||
}
|
||||
|
||||
/* 將串列轉換為陣列 */
|
||||
func toArray() -> [Int] {
|
||||
Array(arr.prefix(size()))
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum _MyList {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* 初始化串列 */
|
||||
let nums = MyList()
|
||||
/* 在尾部新增元素 */
|
||||
nums.add(num: 1)
|
||||
nums.add(num: 3)
|
||||
nums.add(num: 2)
|
||||
nums.add(num: 5)
|
||||
nums.add(num: 4)
|
||||
print("串列 nums = \(nums.toArray()) ,容量 = \(nums.capacity()) ,長度 = \(nums.size())")
|
||||
|
||||
/* 在中間插入元素 */
|
||||
nums.insert(index: 3, num: 6)
|
||||
print("在索引 3 處插入數字 6 ,得到 nums = \(nums.toArray())")
|
||||
|
||||
/* 刪除元素 */
|
||||
nums.remove(index: 3)
|
||||
print("刪除索引 3 處的元素,得到 nums = \(nums.toArray())")
|
||||
|
||||
/* 訪問元素 */
|
||||
let num = nums.get(index: 1)
|
||||
print("訪問索引 1 處的元素,得到 num = \(num)")
|
||||
|
||||
/* 更新元素 */
|
||||
nums.set(index: 1, num: 0)
|
||||
print("將索引 1 處的元素更新為 0 ,得到 nums = \(nums.toArray())")
|
||||
|
||||
/* 測試擴容機制 */
|
||||
for i in 0 ..< 10 {
|
||||
// 在 i = 5 時,串列長度將超出串列容量,此時觸發擴容機制
|
||||
nums.add(num: i)
|
||||
}
|
||||
print("擴容後的串列 nums = \(nums.toArray()) ,容量 = \(nums.capacity()) ,長度 = \(nums.size())")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user