mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-08 21:46:06 +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,97 @@
|
||||
/**
|
||||
* File: array.js
|
||||
* Created Time: 2022-11-27
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
/* 隨機訪問元素 */
|
||||
function randomAccess(nums) {
|
||||
// 在區間 [0, nums.length) 中隨機抽取一個數字
|
||||
const random_index = Math.floor(Math.random() * nums.length);
|
||||
// 獲取並返回隨機元素
|
||||
const random_num = nums[random_index];
|
||||
return random_num;
|
||||
}
|
||||
|
||||
/* 擴展陣列長度 */
|
||||
// 請注意,JavaScript 的 Array 是動態陣列,可以直接擴展
|
||||
// 為了方便學習,本函式將 Array 看作長度不可變的陣列
|
||||
function extend(nums, enlarge) {
|
||||
// 初始化一個擴展長度後的陣列
|
||||
const res = new Array(nums.length + enlarge).fill(0);
|
||||
// 將原陣列中的所有元素複製到新陣列
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// 返回擴展後的新陣列
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 在陣列的索引 index 處插入元素 num */
|
||||
function insert(nums, num, index) {
|
||||
// 把索引 index 以及之後的所有元素向後移動一位
|
||||
for (let i = nums.length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 將 num 賦給 index 處的元素
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 刪除索引 index 處的元素 */
|
||||
function remove(nums, index) {
|
||||
// 把索引 index 之後的所有元素向前移動一位
|
||||
for (let i = index; i < nums.length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* 走訪陣列 */
|
||||
function traverse(nums) {
|
||||
let count = 0;
|
||||
// 透過索引走訪陣列
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
// 直接走訪陣列元素
|
||||
for (const num of nums) {
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
/* 在陣列中查詢指定元素 */
|
||||
function find(nums, target) {
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
if (nums[i] === target) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化陣列 */
|
||||
const arr = new Array(5).fill(0);
|
||||
console.log('陣列 arr =', arr);
|
||||
let nums = [1, 3, 2, 5, 4];
|
||||
console.log('陣列 nums =', nums);
|
||||
|
||||
/* 隨機訪問 */
|
||||
let random_num = randomAccess(nums);
|
||||
console.log('在 nums 中獲取隨機元素', random_num);
|
||||
|
||||
/* 長度擴展 */
|
||||
nums = extend(nums, 3);
|
||||
console.log('將陣列長度擴展至 8 ,得到 nums =', nums);
|
||||
|
||||
/* 插入元素 */
|
||||
insert(nums, 6, 3);
|
||||
console.log('在索引 3 處插入數字 6 ,得到 nums =', nums);
|
||||
|
||||
/* 刪除元素 */
|
||||
remove(nums, 2);
|
||||
console.log('刪除索引 2 處的元素,得到 nums =', nums);
|
||||
|
||||
/* 走訪陣列 */
|
||||
traverse(nums);
|
||||
|
||||
/* 查詢元素 */
|
||||
let index = find(nums, 3);
|
||||
console.log('在 nums 中查詢元素 3 ,得到索引 =', index);
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* File: linked_list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: IsChristina (christinaxia77@foxmail.com), Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { printLinkedList } = require('../modules/PrintUtil');
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
|
||||
/* 在鏈結串列的節點 n0 之後插入節點 P */
|
||||
function insert(n0, P) {
|
||||
const n1 = n0.next;
|
||||
P.next = n1;
|
||||
n0.next = P;
|
||||
}
|
||||
|
||||
/* 刪除鏈結串列的節點 n0 之後的首個節點 */
|
||||
function remove(n0) {
|
||||
if (!n0.next) return;
|
||||
// n0 -> P -> n1
|
||||
const P = n0.next;
|
||||
const n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* 訪問鏈結串列中索引為 index 的節點 */
|
||||
function access(head, index) {
|
||||
for (let i = 0; i < index; i++) {
|
||||
if (!head) {
|
||||
return null;
|
||||
}
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 在鏈結串列中查詢值為 target 的首個節點 */
|
||||
function find(head, target) {
|
||||
let index = 0;
|
||||
while (head !== null) {
|
||||
if (head.val === target) {
|
||||
return index;
|
||||
}
|
||||
head = head.next;
|
||||
index += 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化鏈結串列 */
|
||||
// 初始化各個節點
|
||||
const n0 = new ListNode(1);
|
||||
const n1 = new ListNode(3);
|
||||
const n2 = new ListNode(2);
|
||||
const n3 = new ListNode(5);
|
||||
const n4 = new ListNode(4);
|
||||
// 構建節點之間的引用
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
console.log('初始化的鏈結串列為');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 插入節點 */
|
||||
insert(n0, new ListNode(0));
|
||||
console.log('插入節點後的鏈結串列為');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 刪除節點 */
|
||||
remove(n0);
|
||||
console.log('刪除節點後的鏈結串列為');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 訪問節點 */
|
||||
const node = access(n0, 3);
|
||||
console.log('鏈結串列中索引 3 處的節點的值 = ' + node.val);
|
||||
|
||||
/* 查詢節點 */
|
||||
const index = find(n0, 2);
|
||||
console.log('鏈結串列中值為 2 的節點的索引 = ' + index);
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* File: list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* 初始化串列 */
|
||||
const nums = [1, 3, 2, 5, 4];
|
||||
console.log(`串列 nums = ${nums}`);
|
||||
|
||||
/* 訪問元素 */
|
||||
const num = nums[1];
|
||||
console.log(`訪問索引 1 處的元素,得到 num = ${num}`);
|
||||
|
||||
/* 更新元素 */
|
||||
nums[1] = 0;
|
||||
console.log(`將索引 1 處的元素更新為 0 ,得到 nums = ${nums}`);
|
||||
|
||||
/* 清空串列 */
|
||||
nums.length = 0;
|
||||
console.log(`清空串列後 nums = ${nums}`);
|
||||
|
||||
/* 在尾部新增元素 */
|
||||
nums.push(1);
|
||||
nums.push(3);
|
||||
nums.push(2);
|
||||
nums.push(5);
|
||||
nums.push(4);
|
||||
console.log(`新增元素後 nums = ${nums}`);
|
||||
|
||||
/* 在中間插入元素 */
|
||||
nums.splice(3, 0, 6);
|
||||
console.log(`在索引 3 處插入數字 6 ,得到 nums = ${nums}`);
|
||||
|
||||
/* 刪除元素 */
|
||||
nums.splice(3, 1);
|
||||
console.log(`刪除索引 3 處的元素,得到 nums = ${nums}`);
|
||||
|
||||
/* 透過索引走訪串列 */
|
||||
let count = 0;
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
/* 直接走訪串列元素 */
|
||||
count = 0;
|
||||
for (const x of nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 拼接兩個串列 */
|
||||
const nums1 = [6, 8, 7, 10, 9];
|
||||
nums.push(...nums1);
|
||||
console.log(`將串列 nums1 拼接到 nums 之後,得到 nums = ${nums}`);
|
||||
|
||||
/* 排序串列 */
|
||||
nums.sort((a, b) => a - b);
|
||||
console.log(`排序串列後 nums = ${nums}`);
|
||||
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* File: my_list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* 串列類別 */
|
||||
class MyList {
|
||||
#arr = new Array(); // 陣列(儲存串列元素)
|
||||
#capacity = 10; // 串列容量
|
||||
#size = 0; // 串列長度(當前元素數量)
|
||||
#extendRatio = 2; // 每次串列擴容的倍數
|
||||
|
||||
/* 建構子 */
|
||||
constructor() {
|
||||
this.#arr = new Array(this.#capacity);
|
||||
}
|
||||
|
||||
/* 獲取串列長度(當前元素數量)*/
|
||||
size() {
|
||||
return this.#size;
|
||||
}
|
||||
|
||||
/* 獲取串列容量 */
|
||||
capacity() {
|
||||
return this.#capacity;
|
||||
}
|
||||
|
||||
/* 訪問元素 */
|
||||
get(index) {
|
||||
// 索引如果越界,則丟擲異常,下同
|
||||
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
||||
return this.#arr[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
set(index, num) {
|
||||
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
||||
this.#arr[index] = num;
|
||||
}
|
||||
|
||||
/* 在尾部新增元素 */
|
||||
add(num) {
|
||||
// 如果長度等於容量,則需要擴容
|
||||
if (this.#size === this.#capacity) {
|
||||
this.extendCapacity();
|
||||
}
|
||||
// 將新元素新增到串列尾部
|
||||
this.#arr[this.#size] = num;
|
||||
this.#size++;
|
||||
}
|
||||
|
||||
/* 在中間插入元素 */
|
||||
insert(index, num) {
|
||||
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
||||
// 元素數量超出容量時,觸發擴容機制
|
||||
if (this.#size === this.#capacity) {
|
||||
this.extendCapacity();
|
||||
}
|
||||
// 將索引 index 以及之後的元素都向後移動一位
|
||||
for (let j = this.#size - 1; j >= index; j--) {
|
||||
this.#arr[j + 1] = this.#arr[j];
|
||||
}
|
||||
// 更新元素數量
|
||||
this.#arr[index] = num;
|
||||
this.#size++;
|
||||
}
|
||||
|
||||
/* 刪除元素 */
|
||||
remove(index) {
|
||||
if (index < 0 || index >= this.#size) throw new Error('索引越界');
|
||||
let num = this.#arr[index];
|
||||
// 將將索引 index 之後的元素都向前移動一位
|
||||
for (let j = index; j < this.#size - 1; j++) {
|
||||
this.#arr[j] = this.#arr[j + 1];
|
||||
}
|
||||
// 更新元素數量
|
||||
this.#size--;
|
||||
// 返回被刪除的元素
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 串列擴容 */
|
||||
extendCapacity() {
|
||||
// 新建一個長度為原陣列 extendRatio 倍的新陣列,並將原陣列複製到新陣列
|
||||
this.#arr = this.#arr.concat(
|
||||
new Array(this.capacity() * (this.#extendRatio - 1))
|
||||
);
|
||||
// 更新串列容量
|
||||
this.#capacity = this.#arr.length;
|
||||
}
|
||||
|
||||
/* 將串列轉換為陣列 */
|
||||
toArray() {
|
||||
let size = this.size();
|
||||
// 僅轉換有效長度範圍內的串列元素
|
||||
const arr = new Array(size);
|
||||
for (let i = 0; i < size; i++) {
|
||||
arr[i] = this.get(i);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化串列 */
|
||||
const nums = new MyList();
|
||||
/* 在尾部新增元素 */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
nums.add(5);
|
||||
nums.add(4);
|
||||
console.log(
|
||||
`串列 nums = ${nums.toArray()} ,容量 = ${nums.capacity()} ,長度 = ${nums.size()}`
|
||||
);
|
||||
|
||||
/* 在中間插入元素 */
|
||||
nums.insert(3, 6);
|
||||
console.log(`在索引 3 處插入數字 6 ,得到 nums = ${nums.toArray()}`);
|
||||
|
||||
/* 刪除元素 */
|
||||
nums.remove(3);
|
||||
console.log(`刪除索引 3 處的元素,得到 nums = ${nums.toArray()}`);
|
||||
|
||||
/* 訪問元素 */
|
||||
const num = nums.get(1);
|
||||
console.log(`訪問索引 1 處的元素,得到 num = ${num}`);
|
||||
|
||||
/* 更新元素 */
|
||||
nums.set(1, 0);
|
||||
console.log(`將索引 1 處的元素更新為 0 ,得到 nums = ${nums.toArray()}`);
|
||||
|
||||
/* 測試擴容機制 */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
// 在 i = 5 時,串列長度將超出串列容量,此時觸發擴容機制
|
||||
nums.add(i);
|
||||
}
|
||||
console.log(
|
||||
`擴容後的串列 nums = ${nums.toArray()} ,容量 = ${nums.capacity()} ,長度 = ${nums.size()}`
|
||||
);
|
||||
Reference in New Issue
Block a user