pull/944/head
krahets 1 year ago
parent f8f7086196
commit 7351ec70a6

@ -36,7 +36,7 @@ comments: true
## 16.2.3.   Docker 部署 ## 16.2.3.   Docker 部署
我们可以通过 Docker 来部署本项目。执行以下脚本,稍等片刻后,即可使用浏览器打开 `http://localhost:8000`访问本项目。 执行以下 Docker 脚本,稍等片刻,即可在网页 `http://localhost:8000` 访问本项目。
```shell ```shell
git clone https://github.com/krahets/hello-algo.git git clone https://github.com/krahets/hello-algo.git

@ -51,7 +51,7 @@ comments: true
nums := []int{1, 3, 2, 5, 4} nums := []int{1, 3, 2, 5, 4}
``` ```
=== "JavaScript" === "JS"
```javascript title="array.js" ```javascript title="array.js"
/* 初始化数组 */ /* 初始化数组 */
@ -59,7 +59,7 @@ comments: true
var nums = [1, 3, 2, 5, 4]; var nums = [1, 3, 2, 5, 4];
``` ```
=== "TypeScript" === "TS"
```typescript title="array.ts" ```typescript title="array.ts"
/* 初始化数组 */ /* 初始化数组 */
@ -184,7 +184,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="array.js" ```javascript title="array.js"
/* 随机返回一个数组元素 */ /* 随机返回一个数组元素 */
@ -197,7 +197,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="array.ts" ```typescript title="array.ts"
/* 随机返回一个数组元素 */ /* 随机返回一个数组元素 */
@ -357,7 +357,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="array.js" ```javascript title="array.js"
/* 扩展数组长度 */ /* 扩展数组长度 */
@ -375,7 +375,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="array.ts" ```typescript title="array.ts"
/* 扩展数组长度 */ /* 扩展数组长度 */
@ -552,7 +552,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="array.js" ```javascript title="array.js"
/* 在数组的索引 index 处插入元素 num */ /* 在数组的索引 index 处插入元素 num */
@ -566,7 +566,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="array.ts" ```typescript title="array.ts"
/* 在数组的索引 index 处插入元素 num */ /* 在数组的索引 index 处插入元素 num */
@ -717,7 +717,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="array.js" ```javascript title="array.js"
/* 删除索引 index 处元素 */ /* 删除索引 index 处元素 */
@ -729,7 +729,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="array.ts" ```typescript title="array.ts"
/* 删除索引 index 处元素 */ /* 删除索引 index 处元素 */
@ -891,7 +891,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="array.js" ```javascript title="array.js"
/* 遍历数组 */ /* 遍历数组 */
@ -908,7 +908,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="array.ts" ```typescript title="array.ts"
/* 遍历数组 */ /* 遍历数组 */
@ -1084,7 +1084,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="array.js" ```javascript title="array.js"
/* 在数组中查找指定元素 */ /* 在数组中查找指定元素 */
@ -1096,7 +1096,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="array.ts" ```typescript title="array.ts"
/* 在数组中查找指定元素 */ /* 在数组中查找指定元素 */

@ -64,7 +64,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
/* 链表节点类 */ /* 链表节点类 */
@ -78,7 +78,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
/* 链表节点类 */ /* 链表节点类 */
@ -253,7 +253,7 @@ comments: true
n3.Next = n4 n3.Next = n4
``` ```
=== "JavaScript" === "JS"
```javascript title="linked_list.js" ```javascript title="linked_list.js"
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
@ -270,7 +270,7 @@ comments: true
n3.next = n4; n3.next = n4;
``` ```
=== "TypeScript" === "TS"
```typescript title="linked_list.ts" ```typescript title="linked_list.ts"
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
@ -429,7 +429,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="linked_list.js" ```javascript title="linked_list.js"
/* 在链表的节点 n0 之后插入节点 P */ /* 在链表的节点 n0 之后插入节点 P */
@ -440,7 +440,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="linked_list.ts" ```typescript title="linked_list.ts"
/* 在链表的节点 n0 之后插入节点 P */ /* 在链表的节点 n0 之后插入节点 P */
@ -582,7 +582,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="linked_list.js" ```javascript title="linked_list.js"
/* 删除链表的节点 n0 之后的首个节点 */ /* 删除链表的节点 n0 之后的首个节点 */
@ -595,7 +595,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="linked_list.ts" ```typescript title="linked_list.ts"
/* 删除链表的节点 n0 之后的首个节点 */ /* 删除链表的节点 n0 之后的首个节点 */
@ -758,7 +758,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="linked_list.js" ```javascript title="linked_list.js"
/* 访问链表中索引为 index 的节点 */ /* 访问链表中索引为 index 的节点 */
@ -773,7 +773,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="linked_list.ts" ```typescript title="linked_list.ts"
/* 访问链表中索引为 index 的节点 */ /* 访问链表中索引为 index 的节点 */
@ -941,7 +941,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="linked_list.js" ```javascript title="linked_list.js"
/* 在链表中查找值为 target 的首个节点 */ /* 在链表中查找值为 target 的首个节点 */
@ -958,7 +958,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="linked_list.ts" ```typescript title="linked_list.ts"
/* 在链表中查找值为 target 的首个节点 */ /* 在链表中查找值为 target 的首个节点 */
@ -1134,7 +1134,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
/* 双向链表节点类 */ /* 双向链表节点类 */
@ -1150,7 +1150,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
/* 双向链表节点类 */ /* 双向链表节点类 */

@ -54,7 +54,7 @@ comments: true
list := []int{1, 3, 2, 5, 4} list := []int{1, 3, 2, 5, 4}
``` ```
=== "JavaScript" === "JS"
```javascript title="list.js" ```javascript title="list.js"
/* 初始化列表 */ /* 初始化列表 */
@ -64,7 +64,7 @@ comments: true
const list = [1, 3, 2, 5, 4]; const list = [1, 3, 2, 5, 4];
``` ```
=== "TypeScript" === "TS"
```typescript title="list.ts" ```typescript title="list.ts"
/* 初始化列表 */ /* 初始化列表 */
@ -168,7 +168,7 @@ comments: true
list[1] = 0 // 将索引 1 处的元素更新为 0 list[1] = 0 // 将索引 1 处的元素更新为 0
``` ```
=== "JavaScript" === "JS"
```javascript title="list.js" ```javascript title="list.js"
/* 访问元素 */ /* 访问元素 */
@ -178,7 +178,7 @@ comments: true
list[1] = 0; // 将索引 1 处的元素更新为 0 list[1] = 0; // 将索引 1 处的元素更新为 0
``` ```
=== "TypeScript" === "TS"
```typescript title="list.ts" ```typescript title="list.ts"
/* 访问元素 */ /* 访问元素 */
@ -322,7 +322,7 @@ comments: true
list = append(list[:3], list[4:]...) // 删除索引 3 处的元素 list = append(list[:3], list[4:]...) // 删除索引 3 处的元素
``` ```
=== "JavaScript" === "JS"
```javascript title="list.js" ```javascript title="list.js"
/* 清空列表 */ /* 清空列表 */
@ -342,7 +342,7 @@ comments: true
list.splice(3, 1); list.splice(3, 1);
``` ```
=== "TypeScript" === "TS"
```typescript title="list.ts" ```typescript title="list.ts"
/* 清空列表 */ /* 清空列表 */
@ -518,7 +518,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="list.js" ```javascript title="list.js"
/* 通过索引遍历列表 */ /* 通过索引遍历列表 */
@ -534,7 +534,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="list.ts" ```typescript title="list.ts"
/* 通过索引遍历列表 */ /* 通过索引遍历列表 */
@ -662,7 +662,7 @@ comments: true
list = append(list, list1...) // 将列表 list1 拼接到 list 之后 list = append(list, list1...) // 将列表 list1 拼接到 list 之后
``` ```
=== "JavaScript" === "JS"
```javascript title="list.js" ```javascript title="list.js"
/* 拼接两个列表 */ /* 拼接两个列表 */
@ -670,7 +670,7 @@ comments: true
list.push(...list1); // 将列表 list1 拼接到 list 之后 list.push(...list1); // 将列表 list1 拼接到 list 之后
``` ```
=== "TypeScript" === "TS"
```typescript title="list.ts" ```typescript title="list.ts"
/* 拼接两个列表 */ /* 拼接两个列表 */
@ -754,14 +754,14 @@ comments: true
sort.Ints(list) // 排序后,列表元素从小到大排列 sort.Ints(list) // 排序后,列表元素从小到大排列
``` ```
=== "JavaScript" === "JS"
```javascript title="list.js" ```javascript title="list.js"
/* 排序列表 */ /* 排序列表 */
list.sort((a, b) => a - b); // 排序后,列表元素从小到大排列 list.sort((a, b) => a - b); // 排序后,列表元素从小到大排列
``` ```
=== "TypeScript" === "TS"
```typescript title="list.ts" ```typescript title="list.ts"
/* 排序列表 */ /* 排序列表 */
@ -1224,7 +1224,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="my_list.js" ```javascript title="my_list.js"
/* 列表类简易实现 */ /* 列表类简易实现 */
@ -1326,7 +1326,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="my_list.ts" ```typescript title="my_list.ts"
/* 列表类简易实现 */ /* 列表类简易实现 */

@ -70,7 +70,7 @@ comments: true
if root == nil { if root == nil {
return return
} }
if int(root.Val) == 7 { if (root.Val).(int) == 7 {
// 记录解 // 记录解
*res = append(*res, root) *res = append(*res, root)
} }
@ -79,7 +79,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="preorder_traversal_i_compact.js" ```javascript title="preorder_traversal_i_compact.js"
/* 前序遍历:例题一 */ /* 前序遍历:例题一 */
@ -96,7 +96,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="preorder_traversal_i_compact.ts" ```typescript title="preorder_traversal_i_compact.ts"
/* 前序遍历:例题一 */ /* 前序遍历:例题一 */
@ -272,7 +272,7 @@ comments: true
} }
// 尝试 // 尝试
*path = append(*path, root) *path = append(*path, root)
if int(root.Val) == 7 { if root.Val.(int) == 7 {
// 记录解 // 记录解
*res = append(*res, *path) *res = append(*res, *path)
} }
@ -283,7 +283,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="preorder_traversal_ii_compact.js" ```javascript title="preorder_traversal_ii_compact.js"
/* 前序遍历:例题二 */ /* 前序遍历:例题二 */
@ -304,7 +304,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="preorder_traversal_ii_compact.ts" ```typescript title="preorder_traversal_ii_compact.ts"
/* 前序遍历:例题二 */ /* 前序遍历:例题二 */
@ -539,7 +539,7 @@ comments: true
} }
// 尝试 // 尝试
*path = append(*path, root) *path = append(*path, root)
if int(root.Val) == 7 { if root.Val.(int) == 7 {
// 记录解 // 记录解
*res = append(*res, *path) *res = append(*res, *path)
*path = (*path)[:len(*path)-1] *path = (*path)[:len(*path)-1]
@ -552,7 +552,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="preorder_traversal_iii_compact.js" ```javascript title="preorder_traversal_iii_compact.js"
/* 前序遍历:例题三 */ /* 前序遍历:例题三 */
@ -576,7 +576,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="preorder_traversal_iii_compact.ts" ```typescript title="preorder_traversal_iii_compact.ts"
/* 前序遍历:例题三 */ /* 前序遍历:例题三 */
@ -808,7 +808,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
/* 回溯算法框架 */ /* 回溯算法框架 */
@ -834,7 +834,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
/* 回溯算法框架 */ /* 回溯算法框架 */
@ -1174,7 +1174,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="preorder_traversal_iii_template.js" ```javascript title="preorder_traversal_iii_template.js"
/* 判断当前状态是否为解 */ /* 判断当前状态是否为解 */
@ -1224,7 +1224,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="preorder_traversal_iii_template.ts" ```typescript title="preorder_traversal_iii_template.ts"
/* 判断当前状态是否为解 */ /* 判断当前状态是否为解 */

@ -284,7 +284,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="n_queens.js" ```javascript title="n_queens.js"
/* 回溯算法N 皇后 */ /* 回溯算法N 皇后 */
@ -327,7 +327,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="n_queens.ts" ```typescript title="n_queens.ts"
/* 回溯算法N 皇后 */ /* 回溯算法N 皇后 */

@ -193,7 +193,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="permutations_i.js" ```javascript title="permutations_i.js"
/* 回溯算法:全排列 I */ /* 回溯算法:全排列 I */
@ -227,7 +227,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="permutations_i.ts" ```typescript title="permutations_i.ts"
/* 回溯算法:全排列 I */ /* 回溯算法:全排列 I */
@ -577,7 +577,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="permutations_ii.js" ```javascript title="permutations_ii.js"
/* 回溯算法:全排列 II */ /* 回溯算法:全排列 II */
@ -613,7 +613,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="permutations_ii.ts" ```typescript title="permutations_ii.ts"
/* 回溯算法:全排列 II */ /* 回溯算法:全排列 II */

@ -163,7 +163,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="subset_sum_i_naive.js" ```javascript title="subset_sum_i_naive.js"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
@ -171,7 +171,7 @@ comments: true
[class]{}-[func]{subsetSumINaive} [class]{}-[func]{subsetSumINaive}
``` ```
=== "TypeScript" === "TS"
```typescript title="subset_sum_i_naive.ts" ```typescript title="subset_sum_i_naive.ts"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
@ -499,7 +499,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="subset_sum_i.js" ```javascript title="subset_sum_i.js"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
@ -507,7 +507,7 @@ comments: true
[class]{}-[func]{subsetSumI} [class]{}-[func]{subsetSumI}
``` ```
=== "TypeScript" === "TS"
```typescript title="subset_sum_i.ts" ```typescript title="subset_sum_i.ts"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
@ -850,7 +850,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="subset_sum_ii.js" ```javascript title="subset_sum_ii.js"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
@ -858,7 +858,7 @@ comments: true
[class]{}-[func]{subsetSumII} [class]{}-[func]{subsetSumII}
``` ```
=== "TypeScript" === "TS"
```typescript title="subset_sum_ii.ts" ```typescript title="subset_sum_ii.ts"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}

@ -129,7 +129,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
/* 类 */ /* 类 */
@ -157,7 +157,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
/* 类 */ /* 类 */
@ -347,7 +347,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
function algorithm(n) { function algorithm(n) {
@ -359,7 +359,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
function algorithm(n: number): void { function algorithm(n: number): void {
@ -514,7 +514,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
function constFunc() { function constFunc() {
@ -534,7 +534,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
function constFunc(): number { function constFunc(): number {
@ -758,7 +758,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="space_complexity.js" ```javascript title="space_complexity.js"
/* 常数阶 */ /* 常数阶 */
@ -779,7 +779,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="space_complexity.ts" ```typescript title="space_complexity.ts"
/* 常数阶 */ /* 常数阶 */
@ -1012,7 +1012,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="space_complexity.js" ```javascript title="space_complexity.js"
/* 线性阶 */ /* 线性阶 */
@ -1032,7 +1032,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="space_complexity.ts" ```typescript title="space_complexity.ts"
/* 线性阶 */ /* 线性阶 */
@ -1251,7 +1251,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="space_complexity.js" ```javascript title="space_complexity.js"
/* 线性阶(递归实现) */ /* 线性阶(递归实现) */
@ -1262,7 +1262,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="space_complexity.ts" ```typescript title="space_complexity.ts"
/* 线性阶(递归实现) */ /* 线性阶(递归实现) */
@ -1408,7 +1408,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="space_complexity.js" ```javascript title="space_complexity.js"
/* 平方阶 */ /* 平方阶 */
@ -1429,7 +1429,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="space_complexity.ts" ```typescript title="space_complexity.ts"
/* 平方阶 */ /* 平方阶 */
@ -1618,7 +1618,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="space_complexity.js" ```javascript title="space_complexity.js"
/* 平方阶(递归实现) */ /* 平方阶(递归实现) */
@ -1630,7 +1630,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="space_complexity.ts" ```typescript title="space_complexity.ts"
/* 平方阶(递归实现) */ /* 平方阶(递归实现) */
@ -1786,7 +1786,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="space_complexity.js" ```javascript title="space_complexity.js"
/* 指数阶(建立满二叉树) */ /* 指数阶(建立满二叉树) */
@ -1799,7 +1799,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="space_complexity.ts" ```typescript title="space_complexity.ts"
/* 指数阶(建立满二叉树) */ /* 指数阶(建立满二叉树) */

@ -76,7 +76,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
// 在某运行平台下 // 在某运行平台下
@ -91,7 +91,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
// 在某运行平台下 // 在某运行平台下
@ -269,7 +269,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
// 算法 A 时间复杂度:常数阶 // 算法 A 时间复杂度:常数阶
@ -291,7 +291,7 @@ $$
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
// 算法 A 时间复杂度:常数阶 // 算法 A 时间复杂度:常数阶
@ -484,7 +484,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
function algorithm(n) { function algorithm(n) {
@ -498,7 +498,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
function algorithm(n: number): void{ function algorithm(n: number): void{
@ -701,7 +701,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
function algorithm(n) { function algorithm(n) {
@ -720,7 +720,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
function algorithm(n: number): void { function algorithm(n: number): void {
@ -922,7 +922,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="time_complexity.js" ```javascript title="time_complexity.js"
/* 常数阶 */ /* 常数阶 */
@ -934,7 +934,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 常数阶 */ /* 常数阶 */
@ -1085,7 +1085,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="time_complexity.js" ```javascript title="time_complexity.js"
/* 线性阶 */ /* 线性阶 */
@ -1096,7 +1096,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 线性阶 */ /* 线性阶 */
@ -1245,7 +1245,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="time_complexity.js" ```javascript title="time_complexity.js"
/* 线性阶(遍历数组) */ /* 线性阶(遍历数组) */
@ -1259,7 +1259,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 线性阶(遍历数组) */ /* 线性阶(遍历数组) */
@ -1422,7 +1422,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="time_complexity.js" ```javascript title="time_complexity.js"
/* 平方阶 */ /* 平方阶 */
@ -1438,7 +1438,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 平方阶 */ /* 平方阶 */
@ -1650,7 +1650,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="time_complexity.js" ```javascript title="time_complexity.js"
/* 平方阶(冒泡排序) */ /* 平方阶(冒泡排序) */
@ -1673,7 +1673,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 平方阶(冒泡排序) */ /* 平方阶(冒泡排序) */
@ -1912,7 +1912,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="time_complexity.js" ```javascript title="time_complexity.js"
/* 指数阶(循环实现) */ /* 指数阶(循环实现) */
@ -1931,7 +1931,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 指数阶(循环实现) */ /* 指数阶(循环实现) */
@ -2114,7 +2114,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="time_complexity.js" ```javascript title="time_complexity.js"
/* 指数阶(递归实现) */ /* 指数阶(递归实现) */
@ -2124,7 +2124,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 指数阶(递归实现) */ /* 指数阶(递归实现) */
@ -2261,7 +2261,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="time_complexity.js" ```javascript title="time_complexity.js"
/* 对数阶(循环实现) */ /* 对数阶(循环实现) */
@ -2275,7 +2275,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 对数阶(循环实现) */ /* 对数阶(循环实现) */
@ -2426,7 +2426,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="time_complexity.js" ```javascript title="time_complexity.js"
/* 对数阶(递归实现) */ /* 对数阶(递归实现) */
@ -2436,7 +2436,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 对数阶(递归实现) */ /* 对数阶(递归实现) */
@ -2578,7 +2578,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="time_complexity.js" ```javascript title="time_complexity.js"
/* 线性对数阶 */ /* 线性对数阶 */
@ -2592,7 +2592,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 线性对数阶 */ /* 线性对数阶 */
@ -2776,7 +2776,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="time_complexity.js" ```javascript title="time_complexity.js"
/* 阶乘阶(递归实现) */ /* 阶乘阶(递归实现) */
@ -2791,7 +2791,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="time_complexity.ts" ```typescript title="time_complexity.ts"
/* 阶乘阶(递归实现) */ /* 阶乘阶(递归实现) */
@ -3026,7 +3026,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="worst_best_time_complexity.js" ```javascript title="worst_best_time_complexity.js"
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ /* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
@ -3059,7 +3059,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="worst_best_time_complexity.ts" ```typescript title="worst_best_time_complexity.ts"
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ /* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */

@ -82,14 +82,14 @@ comments: true
var booleans = [5]bool{} var booleans = [5]bool{}
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
// JavaScript 的数组可以自由存储各种基本数据类型和对象 // JavaScript 的数组可以自由存储各种基本数据类型和对象
const array = [0, 0.0, 'a', false]; const array = [0, 0.0, 'a', false];
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
// 使用多种基本数据类型来初始化数组 // 使用多种基本数据类型来初始化数组

@ -169,7 +169,7 @@ status: new
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="binary_search_recur.js" ```javascript title="binary_search_recur.js"
[class]{}-[func]{dfs} [class]{}-[func]{dfs}
@ -177,7 +177,7 @@ status: new
[class]{}-[func]{binarySearch} [class]{}-[func]{binarySearch}
``` ```
=== "TypeScript" === "TS"
```typescript title="binary_search_recur.ts" ```typescript title="binary_search_recur.ts"
[class]{}-[func]{dfs} [class]{}-[func]{dfs}

@ -202,7 +202,7 @@ status: new
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="build_tree.js" ```javascript title="build_tree.js"
[class]{}-[func]{dfs} [class]{}-[func]{dfs}
@ -210,7 +210,7 @@ status: new
[class]{}-[func]{buildTree} [class]{}-[func]{buildTree}
``` ```
=== "TypeScript" === "TS"
```typescript title="build_tree.ts" ```typescript title="build_tree.ts"
[class]{}-[func]{dfs} [class]{}-[func]{dfs}

@ -226,7 +226,7 @@ status: new
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="hanota.js" ```javascript title="hanota.js"
[class]{}-[func]{move} [class]{}-[func]{move}
@ -236,7 +236,7 @@ status: new
[class]{}-[func]{hanota} [class]{}-[func]{hanota}
``` ```
=== "TypeScript" === "TS"
```typescript title="hanota.ts" ```typescript title="hanota.ts"
[class]{}-[func]{move} [class]{}-[func]{move}

@ -123,13 +123,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="min_cost_climbing_stairs_dp.js" ```javascript title="min_cost_climbing_stairs_dp.js"
[class]{}-[func]{minCostClimbingStairsDP} [class]{}-[func]{minCostClimbingStairsDP}
``` ```
=== "TypeScript" === "TS"
```typescript title="min_cost_climbing_stairs_dp.ts" ```typescript title="min_cost_climbing_stairs_dp.ts"
[class]{}-[func]{minCostClimbingStairsDP} [class]{}-[func]{minCostClimbingStairsDP}
@ -309,13 +309,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="min_cost_climbing_stairs_dp.js" ```javascript title="min_cost_climbing_stairs_dp.js"
[class]{}-[func]{minCostClimbingStairsDPComp} [class]{}-[func]{minCostClimbingStairsDPComp}
``` ```
=== "TypeScript" === "TS"
```typescript title="min_cost_climbing_stairs_dp.ts" ```typescript title="min_cost_climbing_stairs_dp.ts"
[class]{}-[func]{minCostClimbingStairsDPComp} [class]{}-[func]{minCostClimbingStairsDPComp}
@ -539,13 +539,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="climbing_stairs_constraint_dp.js" ```javascript title="climbing_stairs_constraint_dp.js"
[class]{}-[func]{climbingStairsConstraintDP} [class]{}-[func]{climbingStairsConstraintDP}
``` ```
=== "TypeScript" === "TS"
```typescript title="climbing_stairs_constraint_dp.ts" ```typescript title="climbing_stairs_constraint_dp.ts"
[class]{}-[func]{climbingStairsConstraintDP} [class]{}-[func]{climbingStairsConstraintDP}

@ -192,13 +192,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="min_path_sum.js" ```javascript title="min_path_sum.js"
[class]{}-[func]{minPathSumDFS} [class]{}-[func]{minPathSumDFS}
``` ```
=== "TypeScript" === "TS"
```typescript title="min_path_sum.ts" ```typescript title="min_path_sum.ts"
[class]{}-[func]{minPathSumDFS} [class]{}-[func]{minPathSumDFS}
@ -416,13 +416,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="min_path_sum.js" ```javascript title="min_path_sum.js"
[class]{}-[func]{minPathSumDFSMem} [class]{}-[func]{minPathSumDFSMem}
``` ```
=== "TypeScript" === "TS"
```typescript title="min_path_sum.ts" ```typescript title="min_path_sum.ts"
[class]{}-[func]{minPathSumDFSMem} [class]{}-[func]{minPathSumDFSMem}
@ -661,13 +661,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="min_path_sum.js" ```javascript title="min_path_sum.js"
[class]{}-[func]{minPathSumDP} [class]{}-[func]{minPathSumDP}
``` ```
=== "TypeScript" === "TS"
```typescript title="min_path_sum.ts" ```typescript title="min_path_sum.ts"
[class]{}-[func]{minPathSumDP} [class]{}-[func]{minPathSumDP}
@ -941,13 +941,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="min_path_sum.js" ```javascript title="min_path_sum.js"
[class]{}-[func]{minPathSumDPComp} [class]{}-[func]{minPathSumDPComp}
``` ```
=== "TypeScript" === "TS"
```typescript title="min_path_sum.ts" ```typescript title="min_path_sum.ts"
[class]{}-[func]{minPathSumDPComp} [class]{}-[func]{minPathSumDPComp}

@ -194,13 +194,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="edit_distance.js" ```javascript title="edit_distance.js"
[class]{}-[func]{editDistanceDP} [class]{}-[func]{editDistanceDP}
``` ```
=== "TypeScript" === "TS"
```typescript title="edit_distance.ts" ```typescript title="edit_distance.ts"
[class]{}-[func]{editDistanceDP} [class]{}-[func]{editDistanceDP}
@ -521,13 +521,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="edit_distance.js" ```javascript title="edit_distance.js"
[class]{}-[func]{editDistanceDPComp} [class]{}-[func]{editDistanceDPComp}
``` ```
=== "TypeScript" === "TS"
```typescript title="edit_distance.ts" ```typescript title="edit_distance.ts"
[class]{}-[func]{editDistanceDPComp} [class]{}-[func]{editDistanceDPComp}

@ -141,7 +141,7 @@ status: new
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="climbing_stairs_backtrack.js" ```javascript title="climbing_stairs_backtrack.js"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
@ -149,7 +149,7 @@ status: new
[class]{}-[func]{climbingStairsBacktrack} [class]{}-[func]{climbingStairsBacktrack}
``` ```
=== "TypeScript" === "TS"
```typescript title="climbing_stairs_backtrack.ts" ```typescript title="climbing_stairs_backtrack.ts"
[class]{}-[func]{backtrack} [class]{}-[func]{backtrack}
@ -399,7 +399,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="climbing_stairs_dfs.js" ```javascript title="climbing_stairs_dfs.js"
[class]{}-[func]{dfs} [class]{}-[func]{dfs}
@ -407,7 +407,7 @@ $$
[class]{}-[func]{climbingStairsDFS} [class]{}-[func]{climbingStairsDFS}
``` ```
=== "TypeScript" === "TS"
```typescript title="climbing_stairs_dfs.ts" ```typescript title="climbing_stairs_dfs.ts"
[class]{}-[func]{dfs} [class]{}-[func]{dfs}
@ -633,7 +633,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="climbing_stairs_dfs_mem.js" ```javascript title="climbing_stairs_dfs_mem.js"
[class]{}-[func]{dfs} [class]{}-[func]{dfs}
@ -641,7 +641,7 @@ $$
[class]{}-[func]{climbingStairsDFSMem} [class]{}-[func]{climbingStairsDFSMem}
``` ```
=== "TypeScript" === "TS"
```typescript title="climbing_stairs_dfs_mem.ts" ```typescript title="climbing_stairs_dfs_mem.ts"
[class]{}-[func]{dfs} [class]{}-[func]{dfs}
@ -864,13 +864,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="climbing_stairs_dp.js" ```javascript title="climbing_stairs_dp.js"
[class]{}-[func]{climbingStairsDP} [class]{}-[func]{climbingStairsDP}
``` ```
=== "TypeScript" === "TS"
```typescript title="climbing_stairs_dp.ts" ```typescript title="climbing_stairs_dp.ts"
[class]{}-[func]{climbingStairsDP} [class]{}-[func]{climbingStairsDP}
@ -1051,13 +1051,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="climbing_stairs_dp.js" ```javascript title="climbing_stairs_dp.js"
[class]{}-[func]{climbingStairsDPComp} [class]{}-[func]{climbingStairsDPComp}
``` ```
=== "TypeScript" === "TS"
```typescript title="climbing_stairs_dp.ts" ```typescript title="climbing_stairs_dp.ts"
[class]{}-[func]{climbingStairsDPComp} [class]{}-[func]{climbingStairsDPComp}

@ -144,13 +144,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="knapsack.js" ```javascript title="knapsack.js"
[class]{}-[func]{knapsackDFS} [class]{}-[func]{knapsackDFS}
``` ```
=== "TypeScript" === "TS"
```typescript title="knapsack.ts" ```typescript title="knapsack.ts"
[class]{}-[func]{knapsackDFS} [class]{}-[func]{knapsackDFS}
@ -368,13 +368,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="knapsack.js" ```javascript title="knapsack.js"
[class]{}-[func]{knapsackDFSMem} [class]{}-[func]{knapsackDFSMem}
``` ```
=== "TypeScript" === "TS"
```typescript title="knapsack.ts" ```typescript title="knapsack.ts"
[class]{}-[func]{knapsackDFSMem} [class]{}-[func]{knapsackDFSMem}
@ -599,13 +599,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="knapsack.js" ```javascript title="knapsack.js"
[class]{}-[func]{knapsackDP} [class]{}-[func]{knapsackDP}
``` ```
=== "TypeScript" === "TS"
```typescript title="knapsack.ts" ```typescript title="knapsack.ts"
[class]{}-[func]{knapsackDP} [class]{}-[func]{knapsackDP}
@ -881,13 +881,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="knapsack.js" ```javascript title="knapsack.js"
[class]{}-[func]{knapsackDPComp} [class]{}-[func]{knapsackDPComp}
``` ```
=== "TypeScript" === "TS"
```typescript title="knapsack.ts" ```typescript title="knapsack.ts"
[class]{}-[func]{knapsackDPComp} [class]{}-[func]{knapsackDPComp}

@ -132,13 +132,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="unbounded_knapsack.js" ```javascript title="unbounded_knapsack.js"
[class]{}-[func]{unboundedKnapsackDP} [class]{}-[func]{unboundedKnapsackDP}
``` ```
=== "TypeScript" === "TS"
```typescript title="unbounded_knapsack.ts" ```typescript title="unbounded_knapsack.ts"
[class]{}-[func]{unboundedKnapsackDP} [class]{}-[func]{unboundedKnapsackDP}
@ -371,13 +371,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="unbounded_knapsack.js" ```javascript title="unbounded_knapsack.js"
[class]{}-[func]{unboundedKnapsackDPComp} [class]{}-[func]{unboundedKnapsackDPComp}
``` ```
=== "TypeScript" === "TS"
```typescript title="unbounded_knapsack.ts" ```typescript title="unbounded_knapsack.ts"
[class]{}-[func]{unboundedKnapsackDPComp} [class]{}-[func]{unboundedKnapsackDPComp}
@ -657,13 +657,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="coin_change.js" ```javascript title="coin_change.js"
[class]{}-[func]{coinChangeDP} [class]{}-[func]{coinChangeDP}
``` ```
=== "TypeScript" === "TS"
```typescript title="coin_change.ts" ```typescript title="coin_change.ts"
[class]{}-[func]{coinChangeDP} [class]{}-[func]{coinChangeDP}
@ -960,13 +960,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="coin_change.js" ```javascript title="coin_change.js"
[class]{}-[func]{coinChangeDPComp} [class]{}-[func]{coinChangeDPComp}
``` ```
=== "TypeScript" === "TS"
```typescript title="coin_change.ts" ```typescript title="coin_change.ts"
[class]{}-[func]{coinChangeDPComp} [class]{}-[func]{coinChangeDPComp}
@ -1227,13 +1227,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="coin_change_ii.js" ```javascript title="coin_change_ii.js"
[class]{}-[func]{coinChangeIIDP} [class]{}-[func]{coinChangeIIDP}
``` ```
=== "TypeScript" === "TS"
```typescript title="coin_change_ii.ts" ```typescript title="coin_change_ii.ts"
[class]{}-[func]{coinChangeIIDP} [class]{}-[func]{coinChangeIIDP}
@ -1465,13 +1465,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="coin_change_ii.js" ```javascript title="coin_change_ii.js"
[class]{}-[func]{coinChangeIIDPComp} [class]{}-[func]{coinChangeIIDPComp}
``` ```
=== "TypeScript" === "TS"
```typescript title="coin_change_ii.ts" ```typescript title="coin_change_ii.ts"
[class]{}-[func]{coinChangeIIDPComp} [class]{}-[func]{coinChangeIIDPComp}

@ -386,7 +386,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="graph_adjacency_matrix.js" ```javascript title="graph_adjacency_matrix.js"
/* 基于邻接矩阵实现的无向图类 */ /* 基于邻接矩阵实现的无向图类 */
@ -478,7 +478,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="graph_adjacency_matrix.ts" ```typescript title="graph_adjacency_matrix.ts"
/* 基于邻接矩阵实现的无向图类 */ /* 基于邻接矩阵实现的无向图类 */
@ -1466,7 +1466,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="graph_adjacency_list.js" ```javascript title="graph_adjacency_list.js"
/* 基于邻接表实现的无向图类 */ /* 基于邻接表实现的无向图类 */
@ -1555,7 +1555,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="graph_adjacency_list.ts" ```typescript title="graph_adjacency_list.ts"
/* 基于邻接表实现的无向图类 */ /* 基于邻接表实现的无向图类 */

@ -154,7 +154,7 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="graph_bfs.js" ```javascript title="graph_bfs.js"
/* 广度优先遍历 BFS */ /* 广度优先遍历 BFS */
@ -185,7 +185,7 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="graph_bfs.ts" ```typescript title="graph_bfs.ts"
/* 广度优先遍历 BFS */ /* 广度优先遍历 BFS */
@ -561,7 +561,7 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="graph_dfs.js" ```javascript title="graph_dfs.js"
/* 深度优先遍历 DFS */ /* 深度优先遍历 DFS */
@ -591,7 +591,7 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="graph_dfs.ts" ```typescript title="graph_dfs.ts"
/* 深度优先遍历 DFS 辅助函数 */ /* 深度优先遍历 DFS 辅助函数 */

@ -192,7 +192,7 @@ status: new
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="fractional_knapsack.js" ```javascript title="fractional_knapsack.js"
[class]{Item}-[func]{} [class]{Item}-[func]{}
@ -200,7 +200,7 @@ status: new
[class]{}-[func]{fractionalKnapsack} [class]{}-[func]{fractionalKnapsack}
``` ```
=== "TypeScript" === "TS"
```typescript title="fractional_knapsack.ts" ```typescript title="fractional_knapsack.ts"
[class]{Item}-[func]{} [class]{Item}-[func]{}

@ -118,13 +118,13 @@ status: new
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="coin_change_greedy.js" ```javascript title="coin_change_greedy.js"
[class]{}-[func]{coinChangeGreedy} [class]{}-[func]{coinChangeGreedy}
``` ```
=== "TypeScript" === "TS"
```typescript title="coin_change_greedy.ts" ```typescript title="coin_change_greedy.ts"
[class]{}-[func]{coinChangeGreedy} [class]{}-[func]{coinChangeGreedy}

@ -187,13 +187,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="max_capacity.js" ```javascript title="max_capacity.js"
[class]{}-[func]{maxCapacity} [class]{}-[func]{maxCapacity}
``` ```
=== "TypeScript" === "TS"
```typescript title="max_capacity.ts" ```typescript title="max_capacity.ts"
[class]{}-[func]{maxCapacity} [class]{}-[func]{maxCapacity}

@ -169,13 +169,13 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="max_product_cutting.js" ```javascript title="max_product_cutting.js"
[class]{}-[func]{maxProductCutting} [class]{}-[func]{maxProductCutting}
``` ```
=== "TypeScript" === "TS"
```typescript title="max_product_cutting.ts" ```typescript title="max_product_cutting.ts"
[class]{}-[func]{maxProductCutting} [class]{}-[func]{maxProductCutting}

@ -228,7 +228,7 @@ index = hash(key) % capacity
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="simple_hash.js" ```javascript title="simple_hash.js"
[class]{}-[func]{addHash} [class]{}-[func]{addHash}
@ -240,7 +240,7 @@ index = hash(key) % capacity
[class]{}-[func]{rotHash} [class]{}-[func]{rotHash}
``` ```
=== "TypeScript" === "TS"
```typescript title="simple_hash.ts" ```typescript title="simple_hash.ts"
[class]{}-[func]{addHash} [class]{}-[func]{addHash}
@ -542,13 +542,13 @@ $$
``` ```
=== "JavaScript" === "JS"
```javascript title="built_in_hash.js" ```javascript title="built_in_hash.js"
``` ```
=== "TypeScript" === "TS"
```typescript title="built_in_hash.ts" ```typescript title="built_in_hash.ts"

@ -465,13 +465,13 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="hash_map_chaining.js" ```javascript title="hash_map_chaining.js"
[class]{HashMapChaining}-[func]{} [class]{HashMapChaining}-[func]{}
``` ```
=== "TypeScript" === "TS"
```typescript title="hash_map_chaining.ts" ```typescript title="hash_map_chaining.ts"
[class]{HashMapChaining}-[func]{} [class]{HashMapChaining}-[func]{}
@ -1321,13 +1321,13 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="hash_map_open_addressing.js" ```javascript title="hash_map_open_addressing.js"
[class]{HashMapOpenAddressing}-[func]{} [class]{HashMapOpenAddressing}-[func]{}
``` ```
=== "TypeScript" === "TS"
```typescript title="hash_map_open_addressing.ts" ```typescript title="hash_map_open_addressing.ts"
[class]{HashMapOpenAddressing}-[func]{} [class]{HashMapOpenAddressing}-[func]{}

@ -126,7 +126,7 @@ comments: true
delete(mapp, 10583) delete(mapp, 10583)
``` ```
=== "JavaScript" === "JS"
```javascript title="hash_map.js" ```javascript title="hash_map.js"
/* 初始化哈希表 */ /* 初始化哈希表 */
@ -148,7 +148,7 @@ comments: true
map.delete(10583); map.delete(10583);
``` ```
=== "TypeScript" === "TS"
```typescript title="hash_map.ts" ```typescript title="hash_map.ts"
/* 初始化哈希表 */ /* 初始化哈希表 */
@ -333,7 +333,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="hash_map.js" ```javascript title="hash_map.js"
/* 遍历哈希表 */ /* 遍历哈希表 */
@ -351,7 +351,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="hash_map.ts" ```typescript title="hash_map.ts"
/* 遍历哈希表 */ /* 遍历哈希表 */
@ -835,7 +835,7 @@ index = hash(key) % capacity
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="array_hash_map.js" ```javascript title="array_hash_map.js"
/* 键值对 Number -> String */ /* 键值对 Number -> String */
@ -924,7 +924,7 @@ index = hash(key) % capacity
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="array_hash_map.ts" ```typescript title="array_hash_map.ts"
/* 键值对 Number -> String */ /* 键值对 Number -> String */

@ -71,7 +71,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="my_heap.js" ```javascript title="my_heap.js"
/* 构造方法,建立空堆或根据输入列表建堆 */ /* 构造方法,建立空堆或根据输入列表建堆 */
@ -85,7 +85,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="my_heap.ts" ```typescript title="my_heap.ts"
/* 构造方法,建立空堆或根据输入列表建堆 */ /* 构造方法,建立空堆或根据输入列表建堆 */

@ -240,13 +240,13 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="heap.js" ```javascript title="heap.js"
// JavaScript 未提供内置 Heap 类 // JavaScript 未提供内置 Heap 类
``` ```
=== "TypeScript" === "TS"
```typescript title="heap.ts" ```typescript title="heap.ts"
// TypeScript 未提供内置 Heap 类 // TypeScript 未提供内置 Heap 类
@ -411,7 +411,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="my_heap.js" ```javascript title="my_heap.js"
/* 获取左子节点索引 */ /* 获取左子节点索引 */
@ -430,7 +430,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="my_heap.ts" ```typescript title="my_heap.ts"
/* 获取左子节点索引 */ /* 获取左子节点索引 */
@ -603,7 +603,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="my_heap.js" ```javascript title="my_heap.js"
/* 访问堆顶元素 */ /* 访问堆顶元素 */
@ -612,7 +612,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="my_heap.ts" ```typescript title="my_heap.ts"
/* 访问堆顶元素 */ /* 访问堆顶元素 */
@ -816,7 +816,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="my_heap.js" ```javascript title="my_heap.js"
/* 元素入堆 */ /* 元素入堆 */
@ -842,7 +842,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="my_heap.ts" ```typescript title="my_heap.ts"
/* 元素入堆 */ /* 元素入堆 */
@ -1229,7 +1229,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="my_heap.js" ```javascript title="my_heap.js"
/* 元素出堆 */ /* 元素出堆 */
@ -1265,7 +1265,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="my_heap.ts" ```typescript title="my_heap.ts"
/* 元素出堆 */ /* 元素出堆 */

@ -159,13 +159,13 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="top_k.js" ```javascript title="top_k.js"
[class]{}-[func]{topKHeap} [class]{}-[func]{topKHeap}
``` ```
=== "TypeScript" === "TS"
```typescript title="top_k.ts" ```typescript title="top_k.ts"
[class]{}-[func]{topKHeap} [class]{}-[func]{topKHeap}
@ -180,7 +180,23 @@ comments: true
=== "C#" === "C#"
```csharp title="top_k.cs" ```csharp title="top_k.cs"
[class]{top_k}-[func]{topKHeap} /* 基于堆查找数组中最大的 k 个元素 */
PriorityQueue<int, int> topKHeap(int[] nums, int k) {
PriorityQueue<int, int> heap = new PriorityQueue<int, int>();
// 将数组的前 k 个元素入堆
for (int i = 0; i < k; i++) {
heap.Enqueue(nums[i], nums[i]);
}
// 从第 k+1 个元素开始,保持堆的长度为 k
for (int i = k; i < nums.Length; i++) {
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
if (nums[i] > heap.Peek()) {
heap.Dequeue();
heap.Enqueue(nums[i], nums[i]);
}
}
return heap;
}
``` ```
=== "Swift" === "Swift"

@ -69,7 +69,7 @@ comments: true
*/ */
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
/* 标题注释,用于标注函数、类、测试样例等 */ /* 标题注释,用于标注函数、类、测试样例等 */
@ -82,7 +82,7 @@ comments: true
*/ */
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
/* 标题注释,用于标注函数、类、测试样例等 */ /* 标题注释,用于标注函数、类、测试样例等 */

@ -135,7 +135,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="binary_search.js" ```javascript title="binary_search.js"
/* 二分查找(双闭区间) */ /* 二分查找(双闭区间) */
@ -160,7 +160,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="binary_search.ts" ```typescript title="binary_search.ts"
/* 二分查找(双闭区间) */ /* 二分查找(双闭区间) */
@ -425,7 +425,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="binary_search.js" ```javascript title="binary_search.js"
/* 二分查找(左闭右开) */ /* 二分查找(左闭右开) */
@ -451,7 +451,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="binary_search.ts" ```typescript title="binary_search.ts"
/* 二分查找(左闭右开) */ /* 二分查找(左闭右开) */

@ -148,7 +148,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="binary_search_edge.js" ```javascript title="binary_search_edge.js"
/* 二分查找最左一个元素 */ /* 二分查找最左一个元素 */
@ -172,7 +172,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="binary_search_edge.ts" ```typescript title="binary_search_edge.ts"
/* 二分查找最左一个元素 */ /* 二分查找最左一个元素 */
@ -408,7 +408,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="binary_search_edge.js" ```javascript title="binary_search_edge.js"
/* 二分查找最右一个元素 */ /* 二分查找最右一个元素 */
@ -432,7 +432,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="binary_search_edge.ts" ```typescript title="binary_search_edge.ts"
/* 二分查找最右一个元素 */ /* 二分查找最右一个元素 */

@ -83,7 +83,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="two_sum.js" ```javascript title="two_sum.js"
/* 方法一:暴力枚举 */ /* 方法一:暴力枚举 */
@ -101,7 +101,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="two_sum.ts" ```typescript title="two_sum.ts"
/* 方法一:暴力枚举 */ /* 方法一:暴力枚举 */
@ -317,7 +317,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="two_sum.js" ```javascript title="two_sum.js"
/* 方法二:辅助哈希表 */ /* 方法二:辅助哈希表 */
@ -336,7 +336,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="two_sum.ts" ```typescript title="two_sum.ts"
/* 方法二:辅助哈希表 */ /* 方法二:辅助哈希表 */

@ -114,7 +114,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="bubble_sort.js" ```javascript title="bubble_sort.js"
/* 冒泡排序 */ /* 冒泡排序 */
@ -134,7 +134,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="bubble_sort.ts" ```typescript title="bubble_sort.ts"
/* 冒泡排序 */ /* 冒泡排序 */
@ -370,7 +370,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="bubble_sort.js" ```javascript title="bubble_sort.js"
/* 冒泡排序(标志优化)*/ /* 冒泡排序(标志优化)*/
@ -393,7 +393,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="bubble_sort.ts" ```typescript title="bubble_sort.ts"
/* 冒泡排序(标志优化)*/ /* 冒泡排序(标志优化)*/

@ -143,7 +143,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="bucket_sort.js" ```javascript title="bucket_sort.js"
/* 桶排序 */ /* 桶排序 */
@ -176,7 +176,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="bucket_sort.ts" ```typescript title="bucket_sort.ts"
/* 桶排序 */ /* 桶排序 */

@ -124,7 +124,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="counting_sort.js" ```javascript title="counting_sort.js"
/* 计数排序 */ /* 计数排序 */
@ -151,7 +151,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="counting_sort.ts" ```typescript title="counting_sort.ts"
/* 计数排序 */ /* 计数排序 */
@ -508,7 +508,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="counting_sort.js" ```javascript title="counting_sort.js"
/* 计数排序 */ /* 计数排序 */
@ -546,7 +546,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="counting_sort.ts" ```typescript title="counting_sort.ts"
/* 计数排序 */ /* 计数排序 */

@ -227,7 +227,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="heap_sort.js" ```javascript title="heap_sort.js"
/* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */ /* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */
@ -270,7 +270,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="heap_sort.ts" ```typescript title="heap_sort.ts"
/* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */ /* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */

@ -98,7 +98,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="insertion_sort.js" ```javascript title="insertion_sort.js"
/* 插入排序 */ /* 插入排序 */
@ -117,7 +117,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="insertion_sort.ts" ```typescript title="insertion_sort.ts"
/* 插入排序 */ /* 插入排序 */

@ -240,7 +240,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="merge_sort.js" ```javascript title="merge_sort.js"
/* 合并左子数组和右子数组 */ /* 合并左子数组和右子数组 */
@ -286,7 +286,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="merge_sort.ts" ```typescript title="merge_sort.ts"
/* 合并左子数组和右子数组 */ /* 合并左子数组和右子数组 */

@ -139,7 +139,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="quick_sort.js" ```javascript title="quick_sort.js"
/* 元素交换 */ /* 元素交换 */
@ -169,7 +169,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="quick_sort.ts" ```typescript title="quick_sort.ts"
/* 元素交换 */ /* 元素交换 */
@ -432,7 +432,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="quick_sort.js" ```javascript title="quick_sort.js"
/* 快速排序 */ /* 快速排序 */
@ -447,7 +447,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="quick_sort.ts" ```typescript title="quick_sort.ts"
/* 快速排序 */ /* 快速排序 */
@ -750,7 +750,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="quick_sort.js" ```javascript title="quick_sort.js"
/* 选取三个元素的中位数 */ /* 选取三个元素的中位数 */
@ -787,7 +787,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="quick_sort.ts" ```typescript title="quick_sort.ts"
/* 选取三个元素的中位数 */ /* 选取三个元素的中位数 */
@ -1138,7 +1138,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="quick_sort.js" ```javascript title="quick_sort.js"
/* 快速排序(尾递归优化) */ /* 快速排序(尾递归优化) */
@ -1159,7 +1159,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="quick_sort.ts" ```typescript title="quick_sort.ts"
/* 快速排序(尾递归优化) */ /* 快速排序(尾递归优化) */

@ -236,7 +236,7 @@ $$
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="radix_sort.js" ```javascript title="radix_sort.js"
/* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */ /* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */
@ -293,7 +293,7 @@ $$
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="radix_sort.ts" ```typescript title="radix_sort.ts"
/* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */ /* 获取元素 num 的第 k 位,其中 exp = 10^(k-1) */

@ -131,7 +131,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="selection_sort.js" ```javascript title="selection_sort.js"
/* 选择排序 */ /* 选择排序 */
@ -152,7 +152,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="selection_sort.ts" ```typescript title="selection_sort.ts"
/* 选择排序 */ /* 选择排序 */

@ -142,7 +142,7 @@ comments: true
isEmpty := deque.Len() == 0 isEmpty := deque.Len() == 0
``` ```
=== "JavaScript" === "JS"
```javascript title="deque.js" ```javascript title="deque.js"
/* 初始化双向队列 */ /* 初始化双向队列 */
@ -180,7 +180,7 @@ comments: true
console.log("双向队列是否为空 = " + isEmpty); console.log("双向队列是否为空 = " + isEmpty);
``` ```
=== "TypeScript" === "TS"
```typescript title="deque.ts" ```typescript title="deque.ts"
/* 初始化双向队列 */ /* 初始化双向队列 */
@ -814,7 +814,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="linkedlist_deque.js" ```javascript title="linkedlist_deque.js"
/* 双向链表节点 */ /* 双向链表节点 */
@ -941,7 +941,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="linkedlist_deque.ts" ```typescript title="linkedlist_deque.ts"
/* 双向链表节点 */ /* 双向链表节点 */
@ -2413,7 +2413,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="array_deque.js" ```javascript title="array_deque.js"
/* 基于环形数组实现的双向队列 */ /* 基于环形数组实现的双向队列 */
@ -2521,7 +2521,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="array_deque.ts" ```typescript title="array_deque.ts"
/* 基于环形数组实现的双向队列 */ /* 基于环形数组实现的双向队列 */

@ -136,7 +136,7 @@ comments: true
isEmpty := queue.Len() == 0 isEmpty := queue.Len() == 0
``` ```
=== "JavaScript" === "JS"
```javascript title="queue.js" ```javascript title="queue.js"
/* 初始化队列 */ /* 初始化队列 */
@ -164,7 +164,7 @@ comments: true
const empty = queue.length === 0; const empty = queue.length === 0;
``` ```
=== "TypeScript" === "TS"
```typescript title="queue.ts" ```typescript title="queue.ts"
/* 初始化队列 */ /* 初始化队列 */
@ -572,7 +572,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="linkedlist_queue.js" ```javascript title="linkedlist_queue.js"
/* 基于链表实现的队列 */ /* 基于链表实现的队列 */
@ -640,7 +640,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="linkedlist_queue.ts" ```typescript title="linkedlist_queue.ts"
/* 基于链表实现的队列 */ /* 基于链表实现的队列 */
@ -1486,7 +1486,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="array_queue.js" ```javascript title="array_queue.js"
/* 基于环形数组实现的队列 */ /* 基于环形数组实现的队列 */
@ -1555,7 +1555,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="array_queue.ts" ```typescript title="array_queue.ts"
/* 基于环形数组实现的队列 */ /* 基于环形数组实现的队列 */

@ -137,7 +137,7 @@ comments: true
isEmpty := len(stack) == 0 isEmpty := len(stack) == 0
``` ```
=== "JavaScript" === "JS"
```javascript title="stack.js" ```javascript title="stack.js"
/* 初始化栈 */ /* 初始化栈 */
@ -164,7 +164,7 @@ comments: true
const is_empty = stack.length === 0; const is_empty = stack.length === 0;
``` ```
=== "TypeScript" === "TS"
```typescript title="stack.ts" ```typescript title="stack.ts"
/* 初始化栈 */ /* 初始化栈 */
@ -545,7 +545,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="linkedlist_stack.js" ```javascript title="linkedlist_stack.js"
/* 基于链表实现的栈 */ /* 基于链表实现的栈 */
@ -602,7 +602,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="linkedlist_stack.ts" ```typescript title="linkedlist_stack.ts"
/* 基于链表实现的栈 */ /* 基于链表实现的栈 */
@ -1264,7 +1264,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="array_stack.js" ```javascript title="array_stack.js"
/* 基于数组实现的栈 */ /* 基于数组实现的栈 */
@ -1308,7 +1308,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="array_stack.ts" ```typescript title="array_stack.ts"
/* 基于数组实现的栈 */ /* 基于数组实现的栈 */

@ -62,7 +62,7 @@ comments: true
tree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15} tree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
/* 二叉树的数组表示 */ /* 二叉树的数组表示 */
@ -70,7 +70,7 @@ comments: true
let tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15]; let tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
/* 二叉树的数组表示 */ /* 二叉树的数组表示 */
@ -403,16 +403,110 @@ comments: true
=== "Go" === "Go"
```go title="array_binary_tree.go" ```go title="array_binary_tree.go"
[class]{arrayBinaryTree}-[func]{} /* 数组表示下的二叉树类 */
type arrayBinaryTree struct {
tree []any
}
/* 构造方法 */
func newArrayBinaryTree(arr []any) *arrayBinaryTree {
return &arrayBinaryTree{
tree: arr,
}
}
/* 节点数量 */
func (abt *arrayBinaryTree) size() int {
return len(abt.tree)
}
/* 获取索引为 i 节点的值 */
func (abt *arrayBinaryTree) val(i int) any {
// 若索引越界,则返回 null ,代表空位
if i < 0 || i >= abt.size() {
return nil
}
return abt.tree[i]
}
/* 获取索引为 i 节点的左子节点的索引 */
func (abt *arrayBinaryTree) left(i int) int {
return 2*i + 1
}
/* 获取索引为 i 节点的右子节点的索引 */
func (abt *arrayBinaryTree) right(i int) int {
return 2*i + 2
}
/* 获取索引为 i 节点的父节点的索引 */
func (abt *arrayBinaryTree) parent(i int) int {
return (i - 1) / 2
}
/* 层序遍历 */
func (abt *arrayBinaryTree) levelOrder() []any {
var res []any
// 直接遍历数组
for i := 0; i < abt.size(); i++ {
if abt.val(i) != nil {
res = append(res, abt.val(i))
}
}
return res
}
/* 深度优先遍历 */
func (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {
// 若为空位,则返回
if abt.val(i) == nil {
return
}
// 前序遍历
if order == "pre" {
*res = append(*res, abt.val(i))
}
abt.dfs(abt.left(i), order, res)
// 中序遍历
if order == "in" {
*res = append(*res, abt.val(i))
}
abt.dfs(abt.right(i), order, res)
// 后序遍历
if order == "post" {
*res = append(*res, abt.val(i))
}
}
/* 前序遍历 */
func (abt *arrayBinaryTree) preOrder() []any {
var res []any
abt.dfs(0, "pre", &res)
return res
}
/* 中序遍历 */
func (abt *arrayBinaryTree) inOrder() []any {
var res []any
abt.dfs(0, "in", &res)
return res
}
/* 后序遍历 */
func (abt *arrayBinaryTree) postOrder() []any {
var res []any
abt.dfs(0, "post", &res)
return res
}
``` ```
=== "JavaScript" === "JS"
```javascript title="array_binary_tree.js" ```javascript title="array_binary_tree.js"
[class]{ArrayBinaryTree}-[func]{} [class]{ArrayBinaryTree}-[func]{}
``` ```
=== "TypeScript" === "TS"
```typescript title="array_binary_tree.ts" ```typescript title="array_binary_tree.ts"
[class]{ArrayBinaryTree}-[func]{} [class]{ArrayBinaryTree}-[func]{}

@ -79,7 +79,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
/* AVL 树节点类 */ /* AVL 树节点类 */
@ -97,7 +97,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
/* AVL 树节点类 */ /* AVL 树节点类 */
@ -271,7 +271,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="avl_tree.js" ```javascript title="avl_tree.js"
/* 获取节点高度 */ /* 获取节点高度 */
@ -288,7 +288,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="avl_tree.ts" ```typescript title="avl_tree.ts"
/* 获取节点高度 */ /* 获取节点高度 */
@ -473,7 +473,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="avl_tree.js" ```javascript title="avl_tree.js"
/* 获取平衡因子 */ /* 获取平衡因子 */
@ -485,7 +485,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="avl_tree.ts" ```typescript title="avl_tree.ts"
/* 获取平衡因子 */ /* 获取平衡因子 */
@ -680,7 +680,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="avl_tree.js" ```javascript title="avl_tree.js"
/* 右旋操作 */ /* 右旋操作 */
@ -698,7 +698,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="avl_tree.ts" ```typescript title="avl_tree.ts"
/* 右旋操作 */ /* 右旋操作 */
@ -917,7 +917,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="avl_tree.js" ```javascript title="avl_tree.js"
/* 左旋操作 */ /* 左旋操作 */
@ -935,7 +935,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="avl_tree.ts" ```typescript title="avl_tree.ts"
/* 左旋操作 */ /* 左旋操作 */
@ -1238,7 +1238,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="avl_tree.js" ```javascript title="avl_tree.js"
/* 执行旋转操作,使该子树重新恢复平衡 */ /* 执行旋转操作,使该子树重新恢复平衡 */
@ -1272,7 +1272,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="avl_tree.ts" ```typescript title="avl_tree.ts"
/* 执行旋转操作,使该子树重新恢复平衡 */ /* 执行旋转操作,使该子树重新恢复平衡 */
@ -1614,9 +1614,9 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
return NewTreeNode(val) return NewTreeNode(val)
} }
/* 1. 查找插入位置,并插入节点 */ /* 1. 查找插入位置,并插入节点 */
if val < node.Val { if val < node.Val.(int) {
node.Left = t.insertHelper(node.Left, val) node.Left = t.insertHelper(node.Left, val)
} else if val > node.Val { } else if val > node.Val.(int) {
node.Right = t.insertHelper(node.Right, val) node.Right = t.insertHelper(node.Right, val)
} else { } else {
// 重复节点不插入,直接返回 // 重复节点不插入,直接返回
@ -1631,7 +1631,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="avl_tree.js" ```javascript title="avl_tree.js"
/* 插入节点 */ /* 插入节点 */
@ -1655,7 +1655,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="avl_tree.ts" ```typescript title="avl_tree.ts"
/* 插入节点 */ /* 插入节点 */
@ -2018,9 +2018,9 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
return nil return nil
} }
/* 1. 查找节点,并删除之 */ /* 1. 查找节点,并删除之 */
if val < node.Val { if val < node.Val.(int) {
node.Left = t.removeHelper(node.Left, val) node.Left = t.removeHelper(node.Left, val)
} else if val > node.Val { } else if val > node.Val.(int) {
node.Right = t.removeHelper(node.Right, val) node.Right = t.removeHelper(node.Right, val)
} else { } else {
if node.Left == nil || node.Right == nil { if node.Left == nil || node.Right == nil {
@ -2041,7 +2041,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
for temp.Left != nil { for temp.Left != nil {
temp = temp.Left temp = temp.Left
} }
node.Right = t.removeHelper(node.Right, temp.Val) node.Right = t.removeHelper(node.Right, temp.Val.(int))
node.Val = temp.Val node.Val = temp.Val
} }
} }
@ -2054,7 +2054,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="avl_tree.js" ```javascript title="avl_tree.js"
/* 删除节点 */ /* 删除节点 */
@ -2094,7 +2094,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="avl_tree.ts" ```typescript title="avl_tree.ts"
/* 删除节点 */ /* 删除节点 */

@ -111,10 +111,10 @@ comments: true
node := bst.root node := bst.root
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
for node != nil { for node != nil {
if node.Val < num { if node.Val.(int) < num {
// 目标节点在 cur 的右子树中 // 目标节点在 cur 的右子树中
node = node.Right node = node.Right
} else if node.Val > num { } else if node.Val.(int) > num {
// 目标节点在 cur 的左子树中 // 目标节点在 cur 的左子树中
node = node.Left node = node.Left
} else { } else {
@ -127,7 +127,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="binary_search_tree.js" ```javascript title="binary_search_tree.js"
/* 查找节点 */ /* 查找节点 */
@ -147,7 +147,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="binary_search_tree.ts" ```typescript title="binary_search_tree.ts"
/* 查找节点 */ /* 查找节点 */
@ -422,7 +422,7 @@ comments: true
return return
} }
pre = cur pre = cur
if cur.Val < num { if cur.Val.(int) < num {
cur = cur.Right cur = cur.Right
} else { } else {
cur = cur.Left cur = cur.Left
@ -430,7 +430,7 @@ comments: true
} }
// 插入节点 // 插入节点
node := NewTreeNode(num) node := NewTreeNode(num)
if pre.Val < num { if pre.Val.(int) < num {
pre.Right = node pre.Right = node
} else { } else {
pre.Left = node pre.Left = node
@ -438,7 +438,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="binary_search_tree.js" ```javascript title="binary_search_tree.js"
/* 插入节点 */ /* 插入节点 */
@ -464,7 +464,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="binary_search_tree.ts" ```typescript title="binary_search_tree.ts"
/* 插入节点 */ /* 插入节点 */
@ -900,7 +900,7 @@ comments: true
break break
} }
pre = cur pre = cur
if cur.Val < num { if cur.Val.(int) < num {
// 待删除节点在右子树中 // 待删除节点在右子树中
cur = cur.Right cur = cur.Right
} else { } else {
@ -940,14 +940,14 @@ comments: true
tmp = tmp.Left tmp = tmp.Left
} }
// 递归删除节点 tmp // 递归删除节点 tmp
bst.remove(tmp.Val) bst.remove(tmp.Val.(int))
// 用 tmp 覆盖 cur // 用 tmp 覆盖 cur
cur.Val = tmp.Val cur.Val = tmp.Val
} }
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="binary_search_tree.js" ```javascript title="binary_search_tree.js"
/* 删除节点 */ /* 删除节点 */
@ -996,7 +996,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="binary_search_tree.ts" ```typescript title="binary_search_tree.ts"
/* 删除节点 */ /* 删除节点 */

@ -60,7 +60,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="" ```javascript title=""
/* 二叉树节点类 */ /* 二叉树节点类 */
@ -71,7 +71,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="" ```typescript title=""
/* 二叉树节点类 */ /* 二叉树节点类 */
@ -265,7 +265,7 @@ comments: true
n2.Right = n5 n2.Right = n5
``` ```
=== "JavaScript" === "JS"
```javascript title="binary_tree.js" ```javascript title="binary_tree.js"
/* 初始化二叉树 */ /* 初始化二叉树 */
@ -282,7 +282,7 @@ comments: true
n2.right = n5; n2.right = n5;
``` ```
=== "TypeScript" === "TS"
```typescript title="binary_tree.ts" ```typescript title="binary_tree.ts"
/* 初始化二叉树 */ /* 初始化二叉树 */
@ -431,7 +431,7 @@ comments: true
n1.Left = n2 n1.Left = n2
``` ```
=== "JavaScript" === "JS"
```javascript title="binary_tree.js" ```javascript title="binary_tree.js"
/* 插入与删除节点 */ /* 插入与删除节点 */
@ -443,7 +443,7 @@ comments: true
n1.left = n2; n1.left = n2;
``` ```
=== "TypeScript" === "TS"
```typescript title="binary_tree.ts" ```typescript title="binary_tree.ts"
/* 插入与删除节点 */ /* 插入与删除节点 */

@ -89,12 +89,12 @@ comments: true
```go title="binary_tree_bfs.go" ```go title="binary_tree_bfs.go"
/* 层序遍历 */ /* 层序遍历 */
func levelOrder(root *TreeNode) []int { func levelOrder(root *TreeNode) []any {
// 初始化队列,加入根节点 // 初始化队列,加入根节点
queue := list.New() queue := list.New()
queue.PushBack(root) queue.PushBack(root)
// 初始化一个切片,用于保存遍历序列 // 初始化一个切片,用于保存遍历序列
nums := make([]int, 0) nums := make([]any, 0)
for queue.Len() > 0 { for queue.Len() > 0 {
// 队列出队 // 队列出队
node := queue.Remove(queue.Front()).(*TreeNode) node := queue.Remove(queue.Front()).(*TreeNode)
@ -113,7 +113,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="binary_tree_bfs.js" ```javascript title="binary_tree_bfs.js"
/* 层序遍历 */ /* 层序遍历 */
@ -132,7 +132,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="binary_tree_bfs.ts" ```typescript title="binary_tree_bfs.ts"
/* 层序遍历 */ /* 层序遍历 */
@ -474,7 +474,7 @@ comments: true
} }
``` ```
=== "JavaScript" === "JS"
```javascript title="binary_tree_dfs.js" ```javascript title="binary_tree_dfs.js"
/* 前序遍历 */ /* 前序遍历 */
@ -505,7 +505,7 @@ comments: true
} }
``` ```
=== "TypeScript" === "TS"
```typescript title="binary_tree_dfs.ts" ```typescript title="binary_tree_dfs.ts"
/* 前序遍历 */ /* 前序遍历 */

Loading…
Cancel
Save