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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@ -159,13 +159,13 @@ comments: true
}
```
=== "JavaScript"
=== "JS"
```javascript title="top_k.js"
[class]{}-[func]{topKHeap}
```
=== "TypeScript"
=== "TS"
```typescript title="top_k.ts"
[class]{}-[func]{topKHeap}
@ -180,7 +180,23 @@ comments: true
=== "C#"
```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"

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@ -137,7 +137,7 @@ comments: true
isEmpty := len(stack) == 0
```
=== "JavaScript"
=== "JS"
```javascript title="stack.js"
/* 初始化栈 */
@ -164,7 +164,7 @@ comments: true
const is_empty = stack.length === 0;
```
=== "TypeScript"
=== "TS"
```typescript title="stack.ts"
/* 初始化栈 */
@ -545,7 +545,7 @@ comments: true
}
```
=== "JavaScript"
=== "JS"
```javascript title="linkedlist_stack.js"
/* 基于链表实现的栈 */
@ -602,7 +602,7 @@ comments: true
}
```
=== "TypeScript"
=== "TS"
```typescript title="linkedlist_stack.ts"
/* 基于链表实现的栈 */
@ -1264,7 +1264,7 @@ comments: true
}
```
=== "JavaScript"
=== "JS"
```javascript title="array_stack.js"
/* 基于数组实现的栈 */
@ -1308,7 +1308,7 @@ comments: true
}
```
=== "TypeScript"
=== "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}
```
=== "JavaScript"
=== "JS"
```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];
```
=== "TypeScript"
=== "TS"
```typescript title=""
/* 二叉树的数组表示 */
@ -403,16 +403,110 @@ comments: true
=== "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"
[class]{ArrayBinaryTree}-[func]{}
```
=== "TypeScript"
=== "TS"
```typescript title="array_binary_tree.ts"
[class]{ArrayBinaryTree}-[func]{}

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

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

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

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

Loading…
Cancel
Save