mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 03:46:28 +08:00
Replace ``js with
``javascript
This commit is contained in:
parent
0407cc720c
commit
22b7d65d20
27 changed files with 127 additions and 92 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -10,3 +10,7 @@ hello-algo.iml
|
|||
.cache/
|
||||
scripts/
|
||||
docs/overrides/
|
||||
|
||||
# build
|
||||
build/
|
||||
site/
|
|
@ -68,7 +68,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
/* 链表结点结构体 */
|
||||
class ListNode {
|
||||
val;
|
||||
|
@ -225,7 +225,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="linked_list.js"
|
||||
```javascript title="linked_list.js"
|
||||
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
||||
// 初始化各个结点
|
||||
const n0 = new ListNode(1);
|
||||
|
@ -385,7 +385,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="linked_list.js"
|
||||
```javascript title="linked_list.js"
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
function insert(n0, P) {
|
||||
let n1 = n0.next;
|
||||
|
@ -537,7 +537,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="linked_list.js"
|
||||
```javascript title="linked_list.js"
|
||||
/* 访问链表中索引为 index 的结点 */
|
||||
function access(head, index) {
|
||||
for (let i = 0; i < index; i++) {
|
||||
|
@ -660,7 +660,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="linked_list.js"
|
||||
```javascript title="linked_list.js"
|
||||
/* 在链表中查找值为 target 的首个结点 */
|
||||
function find(head, target) {
|
||||
let index = 0;
|
||||
|
@ -815,7 +815,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
/* 双向链表结点类 */
|
||||
class ListNode {
|
||||
val;
|
||||
|
|
|
@ -56,7 +56,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="list.js"
|
||||
```javascript title="list.js"
|
||||
/* 初始化列表 */
|
||||
// 无初始值
|
||||
const list1 = [];
|
||||
|
@ -154,7 +154,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="list.js"
|
||||
```javascript title="list.js"
|
||||
/* 访问元素 */
|
||||
const num = list[1]; // 访问索引 1 处的元素
|
||||
|
||||
|
@ -292,7 +292,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="list.js"
|
||||
```javascript title="list.js"
|
||||
/* 清空列表 */
|
||||
list.length = 0;
|
||||
|
||||
|
@ -462,7 +462,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="list.js"
|
||||
```javascript title="list.js"
|
||||
/* 通过索引遍历列表 */
|
||||
let count = 0;
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
|
@ -586,7 +586,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="list.js"
|
||||
```javascript title="list.js"
|
||||
/* 拼接两个列表 */
|
||||
const list1 = [6, 8, 7, 10, 9];
|
||||
list.push(...list1); // 将列表 list1 拼接到 list 之后
|
||||
|
@ -664,7 +664,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="list.js"
|
||||
```javascript title="list.js"
|
||||
/* 排序列表 */
|
||||
list.sort((a, b) => a - b); // 排序后,列表元素从小到大排列
|
||||
```
|
||||
|
@ -835,7 +835,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="my_list.js"
|
||||
```javascript title="my_list.js"
|
||||
/* 列表类简易实现 */
|
||||
class MyList {
|
||||
#nums = new Array(); // 数组(存储列表元素)
|
||||
|
|
|
@ -130,7 +130,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
/* 类 */
|
||||
class Node {
|
||||
val;
|
||||
|
@ -309,7 +309,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
function algorithm(n) {
|
||||
const a = 0; // O(1)
|
||||
const b = new Array(10000); // O(1)
|
||||
|
@ -455,7 +455,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
function constFunc() {
|
||||
// do something
|
||||
return 0;
|
||||
|
@ -622,7 +622,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="space_complexity.js"
|
||||
```javascript title="space_complexity.js"
|
||||
/* 常数阶 */
|
||||
function constant(n) {
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
|
@ -785,7 +785,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="space_complexity.js"
|
||||
```javascript title="space_complexity.js"
|
||||
/* 线性阶 */
|
||||
function linear(n) {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
|
@ -928,7 +928,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="space_complexity.js"
|
||||
```javascript title="space_complexity.js"
|
||||
/* 线性阶(递归实现) */
|
||||
function linearRecur(n) {
|
||||
console.log(`递归 n = ${n}`);
|
||||
|
@ -1031,7 +1031,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="space_complexity.js"
|
||||
```javascript title="space_complexity.js"
|
||||
/* 平方阶 */
|
||||
function quadratic(n) {
|
||||
// 矩阵占用 O(n^2) 空间
|
||||
|
@ -1162,7 +1162,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="space_complexity.js"
|
||||
```javascript title="space_complexity.js"
|
||||
/* 平方阶(递归实现) */
|
||||
function quadraticRecur(n) {
|
||||
if (n <= 0) return 0;
|
||||
|
@ -1272,7 +1272,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="space_complexity.js"
|
||||
```javascript title="space_complexity.js"
|
||||
/* 指数阶(建立满二叉树) */
|
||||
function buildTree(n) {
|
||||
if (n === 0) return null;
|
||||
|
|
|
@ -67,7 +67,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="leetcode_two_sum.js"
|
||||
```javascript title="leetcode_two_sum.js"
|
||||
function twoSumBruteForce(nums, target) {
|
||||
const n = nums.length;
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
|
@ -208,7 +208,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="leetcode_two_sum.js"
|
||||
```javascript title="leetcode_two_sum.js"
|
||||
function twoSumHashTable(nums, target) {
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
let m = {};
|
||||
|
|
|
@ -78,7 +78,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
// 在某运行平台下
|
||||
function algorithm(n) {
|
||||
var a = 2; // 1 ns
|
||||
|
@ -252,7 +252,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
// 算法 A 时间复杂度:常数阶
|
||||
function algorithm_A(n) {
|
||||
console.log(0);
|
||||
|
@ -445,7 +445,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
function algorithm(n){
|
||||
var a = 1; // +1
|
||||
a += 1; // +1
|
||||
|
@ -646,7 +646,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
function algorithm(n) {
|
||||
let a = 1; // +0(技巧 1)
|
||||
a = a + n; // +0(技巧 1)
|
||||
|
@ -826,7 +826,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity.js"
|
||||
```javascript title="time_complexity.js"
|
||||
/* 常数阶 */
|
||||
function constant(n) {
|
||||
let count = 0;
|
||||
|
@ -944,7 +944,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity.js"
|
||||
```javascript title="time_complexity.js"
|
||||
/* 线性阶 */
|
||||
function linear(n) {
|
||||
let count = 0;
|
||||
|
@ -1057,7 +1057,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity.js"
|
||||
```javascript title="time_complexity.js"
|
||||
/* 线性阶(遍历数组) */
|
||||
function arrayTraversal(nums) {
|
||||
let count = 0;
|
||||
|
@ -1181,7 +1181,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity.js"
|
||||
```javascript title="time_complexity.js"
|
||||
/* 平方阶 */
|
||||
function quadratic(n) {
|
||||
let count = 0;
|
||||
|
@ -1333,7 +1333,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity.js"
|
||||
```javascript title="time_complexity.js"
|
||||
/* 平方阶(冒泡排序) */
|
||||
function bubbleSort(nums) {
|
||||
let count = 0; // 计数器
|
||||
|
@ -1524,7 +1524,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity.js"
|
||||
```javascript title="time_complexity.js"
|
||||
/* 指数阶(循环实现) */
|
||||
function exponential(n) {
|
||||
let count = 0,
|
||||
|
@ -1679,7 +1679,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity.js"
|
||||
```javascript title="time_complexity.js"
|
||||
/* 指数阶(递归实现) */
|
||||
function expRecur(n) {
|
||||
if (n == 1) return 1;
|
||||
|
@ -1783,7 +1783,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity.js"
|
||||
```javascript title="time_complexity.js"
|
||||
/* 对数阶(循环实现) */
|
||||
function logarithmic(n) {
|
||||
let count = 0;
|
||||
|
@ -1909,7 +1909,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity.js"
|
||||
```javascript title="time_complexity.js"
|
||||
/* 对数阶(递归实现) */
|
||||
function logRecur(n) {
|
||||
if (n <= 1) return 0;
|
||||
|
@ -2014,7 +2014,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity.js"
|
||||
```javascript title="time_complexity.js"
|
||||
/* 线性对数阶 */
|
||||
function linearLogRecur(n) {
|
||||
if (n <= 1) return 1;
|
||||
|
@ -2156,7 +2156,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="time_complexity.js"
|
||||
```javascript title="time_complexity.js"
|
||||
/* 阶乘阶(递归实现) */
|
||||
function factorialRecur(n) {
|
||||
if (n == 0) return 1;
|
||||
|
@ -2338,7 +2338,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="worst_best_time_complexity.js"
|
||||
```javascript title="worst_best_time_complexity.js"
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
function randomNumbers(n) {
|
||||
const nums = Array(n);
|
||||
|
|
|
@ -83,7 +83,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
/* JavaScript 的数组可以自由存储各种基本数据类型和对象 */
|
||||
const array = [0, 0.0, 'a', false];
|
||||
```
|
||||
|
|
|
@ -141,7 +141,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="graph_adjacency_matrix.js"
|
||||
```javascript title="graph_adjacency_matrix.js"
|
||||
|
||||
```
|
||||
|
||||
|
@ -390,7 +390,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="graph_adjacency_list.js"
|
||||
```javascript title="graph_adjacency_list.js"
|
||||
|
||||
```
|
||||
|
||||
|
|
|
@ -131,7 +131,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="hash_map.js"
|
||||
```javascript title="hash_map.js"
|
||||
/* 初始化哈希表 */
|
||||
const map = new ArrayHashMap();
|
||||
/* 添加操作 */
|
||||
|
@ -309,7 +309,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="hash_map.js"
|
||||
```javascript title="hash_map.js"
|
||||
/* 遍历哈希表 */
|
||||
// 遍历键值对 key->value
|
||||
for (const entry of map.entries()) {
|
||||
|
@ -490,7 +490,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="array_hash_map.js"
|
||||
```javascript title="array_hash_map.js"
|
||||
/* 键值对 Number -> String */
|
||||
class Entry {
|
||||
constructor(key, val) {
|
||||
|
|
|
@ -207,7 +207,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="heap.js"
|
||||
```javascript title="heap.js"
|
||||
// JavaScript 未提供内置 heap 类
|
||||
```
|
||||
|
||||
|
@ -329,7 +329,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="my_heap.js"
|
||||
```javascript title="my_heap.js"
|
||||
#maxHeap;
|
||||
|
||||
/* 构造函数,建立空堆或根据输入列表建堆 */
|
||||
|
@ -465,7 +465,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="my_heap.js"
|
||||
```javascript title="my_heap.js"
|
||||
/* 访问堆顶元素 */
|
||||
peek() {
|
||||
return this.#maxHeap[0];
|
||||
|
@ -586,7 +586,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="my_heap.js"
|
||||
```javascript title="my_heap.js"
|
||||
/* 元素入堆 */
|
||||
push(val) {
|
||||
// 添加结点
|
||||
|
@ -794,7 +794,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="my_heap.js"
|
||||
```javascript title="my_heap.js"
|
||||
/* 元素出堆 */
|
||||
poll() {
|
||||
// 判空处理
|
||||
|
@ -967,7 +967,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="my_heap.js"
|
||||
```javascript title="my_heap.js"
|
||||
/* 构造函数,建立空堆或根据输入列表建堆 */
|
||||
constructor(nums) {
|
||||
// 将列表元素原封不动添加进堆
|
||||
|
|
|
@ -142,7 +142,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
/* 标题注释,用于标注函数、类、测试样例等 */
|
||||
|
||||
// 内容注释,用于详解代码
|
||||
|
|
|
@ -94,7 +94,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="binary_search.js"
|
||||
```javascript title="binary_search.js"
|
||||
/* 二分查找(双闭区间) */
|
||||
function binarySearch(nums, target) {
|
||||
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
|
||||
|
@ -243,7 +243,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="binary_search.js"
|
||||
```javascript title="binary_search.js"
|
||||
/* 二分查找(左闭右开) */
|
||||
function binarySearch1(nums, target) {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
|
@ -400,7 +400,7 @@ $$
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
// (i + j) 有可能超出 int 的取值范围
|
||||
let m = parseInt((i + j) / 2);
|
||||
// 更换为此写法则不会越界
|
||||
|
|
|
@ -51,7 +51,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="hashing_search.js"
|
||||
```javascript title="hashing_search.js"
|
||||
/* 哈希查找(数组) */
|
||||
function hashingSearchArray(map, target) {
|
||||
// 哈希表的 key: 目标元素,value: 索引
|
||||
|
@ -145,7 +145,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="hashing_search.js"
|
||||
```javascript title="hashing_search.js"
|
||||
/* 哈希查找(链表) */
|
||||
function hashingSearchLinkedList(map, target) {
|
||||
// 哈希表的 key: 目标结点值,value: 结点对象
|
||||
|
|
|
@ -49,7 +49,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="linear_search.js"
|
||||
```javascript title="linear_search.js"
|
||||
/* 线性查找(数组) */
|
||||
function linearSearchArray(nums, target) {
|
||||
// 遍历数组
|
||||
|
@ -170,7 +170,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="linear_search.js"
|
||||
```javascript title="linear_search.js"
|
||||
/* 线性查找(链表)*/
|
||||
function linearSearchLinkedList(head, target) {
|
||||
// 遍历链表
|
||||
|
|
|
@ -85,7 +85,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="bubble_sort.js"
|
||||
```javascript title="bubble_sort.js"
|
||||
/* 冒泡排序 */
|
||||
function bubbleSort(nums) {
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
|
@ -256,7 +256,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="bubble_sort.js"
|
||||
```javascript title="bubble_sort.js"
|
||||
/* 冒泡排序(标志优化)*/
|
||||
function bubbleSortWithFlag(nums) {
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
|
|
|
@ -63,7 +63,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="insertion_sort.js"
|
||||
```javascript title="insertion_sort.js"
|
||||
/* 插入排序 */
|
||||
function insertionSort(nums) {
|
||||
// 外循环:base = nums[1], nums[2], ..., nums[n-1]
|
||||
|
|
|
@ -171,7 +171,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="merge_sort.js"
|
||||
```javascript title="merge_sort.js"
|
||||
/* 合并左子数组和右子数组 */
|
||||
// 左子数组区间 [left, mid]
|
||||
// 右子数组区间 [mid + 1, right]
|
||||
|
|
|
@ -287,7 +287,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="quick_sort.js"
|
||||
```javascript title="quick_sort.js"
|
||||
/* 快速排序 */
|
||||
function quickSort(nums, left, right) {
|
||||
// 子数组长度为 1 时终止递归
|
||||
|
@ -444,7 +444,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="quick_sort.js"
|
||||
```javascript title="quick_sort.js"
|
||||
/* 选取三个元素的中位数 */
|
||||
function medianThree(nums, left, mid, right) {
|
||||
// 使用了异或操作来简化代码
|
||||
|
@ -606,7 +606,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="quick_sort.js"
|
||||
```javascript title="quick_sort.js"
|
||||
/* 快速排序(尾递归优化) */
|
||||
function quickSort(nums, left, right) {
|
||||
// 子数组长度为 1 时终止
|
||||
|
|
|
@ -148,7 +148,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="deque.js"
|
||||
```javascript title="deque.js"
|
||||
/* 初始化双向队列 */
|
||||
// JavaScript 没有内置的双端队列,只能把 Array 当作双端队列来使用
|
||||
const deque = [];
|
||||
|
@ -346,7 +346,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="linkedlist_deque.js"
|
||||
```javascript title="linkedlist_deque.js"
|
||||
/* 双向链表结点 */
|
||||
class ListNode {
|
||||
prev; // 前驱结点引用 (指针)
|
||||
|
|
|
@ -142,7 +142,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="queue.js"
|
||||
```javascript title="queue.js"
|
||||
/* 初始化队列 */
|
||||
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
const queue = [];
|
||||
|
@ -352,7 +352,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="linkedlist_queue.js"
|
||||
```javascript title="linkedlist_queue.js"
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue {
|
||||
#front; // 头结点 #front
|
||||
|
@ -697,7 +697,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="array_queue.js"
|
||||
```javascript title="array_queue.js"
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue {
|
||||
#nums; // 用于存储队列元素的数组
|
||||
|
|
|
@ -144,7 +144,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="stack.js"
|
||||
```javascript title="stack.js"
|
||||
/* 初始化栈 */
|
||||
// Javascript 没有内置的栈类,可以把 Array 当作栈来使用
|
||||
const stack = [];
|
||||
|
@ -355,7 +355,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="linkedlist_stack.js"
|
||||
```javascript title="linkedlist_stack.js"
|
||||
/* 基于链表实现的栈 */
|
||||
class LinkedListStack {
|
||||
#stackPeek; // 将头结点作为栈顶
|
||||
|
@ -671,7 +671,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="array_stack.js"
|
||||
```javascript title="array_stack.js"
|
||||
/* 基于数组实现的栈 */
|
||||
class ArrayStack {
|
||||
stack;
|
||||
|
|
|
@ -79,7 +79,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
class TreeNode {
|
||||
val; // 结点值
|
||||
height; //结点高度
|
||||
|
@ -214,7 +214,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="avl_tree.js"
|
||||
```javascript title="avl_tree.js"
|
||||
/* 获取结点高度 */
|
||||
height(node) {
|
||||
// 空结点高度为 -1 ,叶结点高度为 0
|
||||
|
@ -328,7 +328,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="avl_tree.js"
|
||||
```javascript title="avl_tree.js"
|
||||
/* 获取平衡因子 */
|
||||
balanceFactor(node) {
|
||||
// 空结点平衡因子为 0
|
||||
|
@ -459,7 +459,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="avl_tree.js"
|
||||
```javascript title="avl_tree.js"
|
||||
/* 右旋操作 */
|
||||
rightRotate(node) {
|
||||
const child = node.left;
|
||||
|
@ -592,7 +592,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="avl_tree.js"
|
||||
```javascript title="avl_tree.js"
|
||||
/* 左旋操作 */
|
||||
leftRotate(node) {
|
||||
const child = node.right;
|
||||
|
@ -766,7 +766,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="avl_tree.js"
|
||||
```javascript title="avl_tree.js"
|
||||
/* 执行旋转操作,使该子树重新恢复平衡 */
|
||||
rotate(node) {
|
||||
// 获取结点 node 的平衡因子
|
||||
|
@ -1003,7 +1003,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="avl_tree.js"
|
||||
```javascript title="avl_tree.js"
|
||||
/* 插入结点 */
|
||||
insert(val) {
|
||||
this.root = this.insertHelper(this.root, val);
|
||||
|
@ -1245,7 +1245,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="avl_tree.js"
|
||||
```javascript title="avl_tree.js"
|
||||
/* 删除结点 */
|
||||
remove(val) {
|
||||
this.root = this.removeHelper(this.root, val);
|
||||
|
|
|
@ -79,7 +79,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="binary_search_tree.js"
|
||||
```javascript title="binary_search_tree.js"
|
||||
/* 查找结点 */
|
||||
function search(num) {
|
||||
let cur = root;
|
||||
|
@ -244,7 +244,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="binary_search_tree.js"
|
||||
```javascript title="binary_search_tree.js"
|
||||
/* 插入结点 */
|
||||
function insert(num) {
|
||||
// 若树为空,直接提前返回
|
||||
|
@ -517,7 +517,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="binary_search_tree.js"
|
||||
```javascript title="binary_search_tree.js"
|
||||
/* 删除结点 */
|
||||
function remove(num) {
|
||||
// 若树为空,直接提前返回
|
||||
|
|
|
@ -62,7 +62,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
/* 链表结点类 */
|
||||
function TreeNode(val, left, right) {
|
||||
this.val = (val === undefined ? 0 : val); // 结点值
|
||||
|
@ -229,7 +229,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="binary_tree.js"
|
||||
```javascript title="binary_tree.js"
|
||||
/* 初始化二叉树 */
|
||||
// 初始化结点
|
||||
let n1 = new TreeNode(1),
|
||||
|
@ -361,7 +361,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="binary_tree.js"
|
||||
```javascript title="binary_tree.js"
|
||||
/* 插入与删除结点 */
|
||||
let P = new TreeNode(0);
|
||||
// 在 n1 -> n2 中间插入结点 P
|
||||
|
@ -527,7 +527,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
```javascript title=""
|
||||
/* 二叉树的数组表示 */
|
||||
// 直接使用 null 来表示空位
|
||||
let tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
|
||||
|
|
|
@ -66,7 +66,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="binary_tree_bfs.js"
|
||||
```javascript title="binary_tree_bfs.js"
|
||||
/* 层序遍历 */
|
||||
function hierOrder(root) {
|
||||
// 初始化队列,加入根结点
|
||||
|
@ -257,7 +257,7 @@ comments: true
|
|||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="binary_tree_dfs.js"
|
||||
```javascript title="binary_tree_dfs.js"
|
||||
/* 前序遍历 */
|
||||
function preOrder(root){
|
||||
if (root === null) return;
|
||||
|
|
|
@ -13,6 +13,7 @@ import shutil
|
|||
from docs.utils.extract_code_python import ExtractCodeBlocksPython
|
||||
from docs.utils.extract_code_java import ExtractCodeBlocksJava
|
||||
from docs.utils.extract_code_cpp import ExtractCodeBlocksCpp
|
||||
from docs.utils.extract_code_jsts import ExtractCodeBlocksJSTS
|
||||
|
||||
|
||||
def build_markdown(md_path):
|
||||
|
@ -85,6 +86,8 @@ extractor_dict = {
|
|||
"java": ExtractCodeBlocksJava(),
|
||||
"python": ExtractCodeBlocksPython(),
|
||||
"cpp": ExtractCodeBlocksCpp(),
|
||||
"javascript": ExtractCodeBlocksJSTS(),
|
||||
"typescript": ExtractCodeBlocksJSTS(),
|
||||
}
|
||||
|
||||
|
||||
|
|
28
docs/utils/extract_code_jsts.py
Normal file
28
docs/utils/extract_code_jsts.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
"""
|
||||
File: extract_code_python.py
|
||||
Created Time: 2023-02-07
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import re
|
||||
import glob
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.dirname(osp.abspath(__file__)))))
|
||||
from docs.utils.extract_code_java import ExtractCodeBlocksJava
|
||||
|
||||
|
||||
class ExtractCodeBlocksJSTS(ExtractCodeBlocksJava):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
# Pattern to match function names and class names
|
||||
self.func_pattern = r'\s*(function|private|public|)\s*(\S*)\(.*\)(:|)\s*(\S*)\s*{\s*\n'
|
||||
self.class_pattern = r'class\s+(\w+)\s*\{'
|
||||
|
||||
self.func_pattern_keys = ["total", "prefix", "label", ":", "return"]
|
||||
self.class_pattern_keys = ["total", "label"]
|
||||
|
||||
|
||||
# for code_path in glob.glob("codes/cpp/chapter_*/my_heap.cpp"):
|
||||
# ext = ExtractCodeBlocksCpp()
|
||||
# ext.extract(code_path)
|
Loading…
Reference in a new issue