feat: Revised the book (#978)

* Sync recent changes to the revised Word.

* Revised the preface chapter

* Revised the introduction chapter

* Revised the computation complexity chapter

* Revised the chapter data structure

* Revised the chapter array and linked list

* Revised the chapter stack and queue

* Revised the chapter hashing

* Revised the chapter tree

* Revised the chapter heap

* Revised the chapter graph

* Revised the chapter searching

* Reivised the sorting chapter

* Revised the divide and conquer chapter

* Revised the chapter backtacking

* Revised the DP chapter

* Revised the greedy chapter

* Revised the appendix chapter

* Revised the preface chapter doubly

* Revised the figures
This commit is contained in:
Yudong Jin
2023-12-02 06:21:34 +08:00
committed by GitHub
parent b824d149cb
commit e720aa2d24
404 changed files with 1537 additions and 1558 deletions

View File

@@ -15,7 +15,7 @@ function randomAccess(nums: number[]): number {
/* 扩展数组长度 */
// 请注意TypeScript 的 Array 是动态数组,可以直接扩展
// 为了方便学习,本函数将 Array 看作长度不可变的数组
// 为了方便学习,本函数将 Array 看作长度不可变的数组
function extend(nums: number[], enlarge: number): number[] {
// 初始化一个扩展长度后的数组
const res = new Array(nums.length + enlarge).fill(0);
@@ -33,11 +33,11 @@ function insert(nums: number[], num: number, index: number): void {
for (let i = nums.length - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// 将 num 赋给 index 处元素
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
/* 删除索引 index 处元素 */
function remove(nums: number[], index: number): void {
// 把索引 index 之后的所有元素向前移动一位
for (let i = index; i < nums.length - 1; i++) {

View File

@@ -57,7 +57,7 @@ const n1 = new ListNode(3);
const n2 = new ListNode(2);
const n3 = new ListNode(5);
const n4 = new ListNode(4);
// 构建引用指向
// 构建节点之间的引用
n0.next = n1;
n1.next = n2;
n2.next = n3;

View File

@@ -20,7 +20,7 @@ console.log(`将索引 1 处的元素更新为 0 ,得到 nums = ${nums}`);
nums.length = 0;
console.log(`清空列表后 nums = ${nums}`);
/* 尾部添加元素 */
/* 尾部添加元素 */
nums.push(1);
nums.push(3);
nums.push(2);
@@ -28,7 +28,7 @@ nums.push(5);
nums.push(4);
console.log(`添加元素后 nums = ${nums}`);
/* 中间插入元素 */
/* 中间插入元素 */
nums.splice(3, 0, 6);
console.log(`在索引 3 处插入数字 6 ,得到 nums = ${nums}`);

View File

@@ -4,11 +4,11 @@
* Author: Justin (xiefahit@gmail.com)
*/
/* 列表类简易实现 */
/* 列表类 */
class MyList {
private arr: Array<number>; // 数组(存储列表元素)
private _capacity: number = 10; // 列表容量
private _size: number = 0; // 列表长度(当前元素数量)
private _size: number = 0; // 列表长度(当前元素数量)
private extendRatio: number = 2; // 每次列表扩容的倍数
/* 构造方法 */
@@ -16,7 +16,7 @@ class MyList {
this.arr = new Array(this._capacity);
}
/* 获取列表长度(当前元素数量)*/
/* 获取列表长度(当前元素数量)*/
public size(): number {
return this._size;
}
@@ -39,7 +39,7 @@ class MyList {
this.arr[index] = num;
}
/* 尾部添加元素 */
/* 尾部添加元素 */
public add(num: number): void {
// 如果长度等于容量,则需要扩容
if (this._size === this._capacity) this.extendCapacity();
@@ -48,7 +48,7 @@ class MyList {
this._size++;
}
/* 中间插入元素 */
/* 中间插入元素 */
public insert(index: number, num: number): void {
if (index < 0 || index >= this._size) throw new Error('索引越界');
// 元素数量超出容量时,触发扩容机制
@@ -103,7 +103,7 @@ class MyList {
/* Driver Code */
/* 初始化列表 */
const nums = new MyList();
/* 尾部添加元素 */
/* 尾部添加元素 */
nums.add(1);
nums.add(3);
nums.add(2);
@@ -113,7 +113,7 @@ console.log(
`列表 nums = ${nums.toArray()} ,容量 = ${nums.capacity()} ,长度 = ${nums.size()}`
);
/* 中间插入元素 */
/* 中间插入元素 */
nums.insert(3, 6);
console.log(`在索引 3 处插入数字 6 ,得到 nums = ${nums.toArray()}`);

View File

@@ -24,7 +24,7 @@ function backtrack(
// 计算该格子对应的主对角线和副对角线
const diag1 = row - col + n - 1;
const diag2 = row + col;
// 剪枝:不允许该格子所在列、主对角线、副对角线存在皇后
// 剪枝:不允许该格子所在列、主对角线、副对角线存在皇后
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state[row][col] = 'Q';
@@ -43,8 +43,8 @@ function nQueens(n: number): string[][][] {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
const state = Array.from({ length: n }, () => Array(n).fill('#'));
const cols = Array(n).fill(false); // 记录列是否有皇后
const diags1 = Array(2 * n - 1).fill(false); // 记录主对角线是否有皇后
const diags2 = Array(2 * n - 1).fill(false); // 记录副对角线是否有皇后
const diags1 = Array(2 * n - 1).fill(false); // 记录主对角线是否有皇后
const diags2 = Array(2 * n - 1).fill(false); // 记录副对角线是否有皇后
const res: string[][][] = [];
backtrack(0, n, state, res, cols, diags1, diags2);

View File

@@ -30,7 +30,7 @@ function whileLoop(n: number): number {
function whileLoopII(n: number): number {
let res = 0;
let i = 1; // 初始化条件变量
// 循环求和 1, 4, ...
// 循环求和 1, 4, 10, ...
while (i <= n) {
res += i;
// 更新条件变量

View File

@@ -12,7 +12,7 @@ function move(src: number[], tar: number[]): void {
tar.push(pan);
}
/* 求解汉诺塔问题 f(i) */
/* 求解汉诺塔问题 f(i) */
function dfs(i: number, src: number[], buf: number[], tar: number[]): void {
// 若 src 只剩下一个圆盘,则直接将其移到 tar
if (i === 1) {
@@ -27,7 +27,7 @@ function dfs(i: number, src: number[], buf: number[], tar: number[]): void {
dfs(i - 1, buf, src, tar);
}
/* 求解汉诺塔 */
/* 求解汉诺塔问题 */
function solveHanota(A: number[], B: number[], C: number[]): void {
const n = A.length;
// 将 A 顶部 n 个圆盘借助 B 移到 C

View File

@@ -25,7 +25,7 @@ function backtrack(
/* 爬楼梯:回溯 */
function climbingStairsBacktrack(n: number): number {
const choices = [1, 2]; // 可选择向上爬 1 或 2 阶
const choices = [1, 2]; // 可选择向上爬 1 或 2 阶
const state = 0; // 从第 0 阶开始爬
const res = new Map();
res.set(0, 0); // 使用 res[0] 记录方案数量

View File

@@ -16,7 +16,7 @@ function coinChangeDP(coins: Array<number>, amt: number): number {
for (let a = 1; a <= amt; a++) {
dp[0][a] = MAX;
}
// 状态转移:其余行列
// 状态转移:其余行
for (let i = 1; i <= n; i++) {
for (let a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {

View File

@@ -74,7 +74,7 @@ function editDistanceDP(s: string, t: string): number {
for (let j = 1; j <= m; j++) {
dp[0][j] = j;
}
// 状态转移:其余行列
// 状态转移:其余行
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= m; j++) {
if (s.charAt(i - 1) === t.charAt(j - 1)) {

View File

@@ -11,11 +11,11 @@ function knapsackDFS(
i: number,
c: number
): number {
// 若已选完所有物品或背包无容量,则返回价值 0
// 若已选完所有物品或背包无剩余容量,则返回价值 0
if (i === 0 || c === 0) {
return 0;
}
// 若超过背包容量,则只能不放入背包
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFS(wgt, val, i - 1, c);
}
@@ -34,7 +34,7 @@ function knapsackDFSMem(
i: number,
c: number
): number {
// 若已选完所有物品或背包无容量,则返回价值 0
// 若已选完所有物品或背包无剩余容量,则返回价值 0
if (i === 0 || c === 0) {
return 0;
}
@@ -42,7 +42,7 @@ function knapsackDFSMem(
if (mem[i][c] !== -1) {
return mem[i][c];
}
// 若超过背包容量,则只能不放入背包
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFSMem(wgt, val, mem, i - 1, c);
}

View File

@@ -69,7 +69,7 @@ function minPathSumDP(grid: Array<Array<number>>): number {
for (let i = 1; i < n; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
// 状态转移:其余行列
// 状态转移:其余行
for (let i = 1; i < n; i++) {
for (let j: number = 1; j < m; j++) {
dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];

View File

@@ -8,7 +8,7 @@ import { Vertex } from '../modules/Vertex';
/* 基于邻接表实现的无向图类 */
class GraphAdjList {
// 邻接表key: 顶点value该顶点的所有邻接顶点
// 邻接表key顶点value该顶点的所有邻接顶点
adjList: Map<Vertex, Vertex[]>;
/* 构造方法 */

View File

@@ -69,7 +69,7 @@ class GraphAdjMat {
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
throw new RangeError('Index Out Of Bounds Exception');
}
// 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) === (j, i)
// 在无向图中,邻接矩阵关于主对角线对称,即满足 (i, j) === (j, i)
this.adjMat[i][j] = 1;
this.adjMat[j][i] = 1;
}

View File

@@ -24,7 +24,7 @@ function graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {
// 遍历该顶点的所有邻接顶点
for (const adjVet of graph.adjList.get(vet) ?? []) {
if (visited.has(adjVet)) {
continue; // 跳过已被访问的顶点
continue; // 跳过已被访问的顶点
}
que.push(adjVet); // 只入队未访问
visited.add(adjVet); // 标记该顶点已被访问

View File

@@ -19,7 +19,7 @@ function dfs(
// 遍历该顶点的所有邻接顶点
for (const adjVet of graph.adjList.get(vet)) {
if (visited.has(adjVet)) {
continue; // 跳过已被访问的顶点
continue; // 跳过已被访问的顶点
}
// 递归访问邻接顶点
dfs(graph, visited, res, adjVet);

View File

@@ -15,7 +15,7 @@ class Pair {
}
}
/* 基于数组简易实现的哈希表 */
/* 基于数组实现的哈希表 */
class ArrayHashMap {
private readonly buckets: (Pair | null)[];

View File

@@ -82,7 +82,7 @@ class MaxHeap {
public pop(): number {
// 判空处理
if (this.isEmpty()) throw new RangeError('Heap is empty.');
// 交换根节点与最右叶节点(交换首元素与尾元素)
// 交换根节点与最右叶节点(交换首元素与尾元素)
this.swap(0, this.size() - 1);
// 删除节点
const val = this.maxHeap.pop();

View File

@@ -27,9 +27,9 @@ function binarySearch(nums: number[], target: number): number {
return -1; // 未找到目标元素,返回 -1
}
/* 二分查找(左闭右开) */
/* 二分查找(左闭右开区间 */
function binarySearchLCRO(nums: number[], target: number): number {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
// 初始化左闭右开区间 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
let i = 0,
j = nums.length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
@@ -58,7 +58,7 @@ const nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
let index = binarySearch(nums, target);
console.info('目标元素 6 的索引 = %d', index);
/* 二分查找(左闭右开) */
/* 二分查找(左闭右开区间 */
index = binarySearchLCRO(nums, target);
console.info('目标元素 6 的索引 = %d', index);

View File

@@ -7,7 +7,7 @@
/* 方法一:暴力枚举 */
function twoSumBruteForce(nums: number[], target: number): number[] {
const n = nums.length;
// 两层循环,时间复杂度 O(n^2)
// 两层循环,时间复杂度 O(n^2)
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
@@ -20,9 +20,9 @@ function twoSumBruteForce(nums: number[], target: number): number[] {
/* 方法二:辅助哈希表 */
function twoSumHashTable(nums: number[], target: number): number[] {
// 辅助哈希表,空间复杂度 O(n)
// 辅助哈希表,空间复杂度 O(n)
let m: Map<number, number> = new Map();
// 单层循环,时间复杂度 O(n)
// 单层循环,时间复杂度 O(n)
for (let i = 0; i < nums.length; i++) {
let index = m.get(target - nums[i]);
if (index !== undefined) {

View File

@@ -14,7 +14,7 @@ function bucketSort(nums: number[]): void {
}
// 1. 将数组元素分配到各个桶中
for (const num of nums) {
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
const i = Math.floor(num * k);
// 将 num 添加进桶 i
buckets[i].push(num);

View File

@@ -36,7 +36,7 @@ function heapSort(nums: number[]): void {
}
// 从堆中提取最大元素,循环 n-1 轮
for (let i = nums.length - 1; i > 0; i--) {
// 交换根节点与最右叶节点(交换首元素与尾元素)
// 交换根节点与最右叶节点(交换首元素与尾元素)
[nums[0], nums[i]] = [nums[i], nums[0]];
// 以根节点为起点,从顶至底进行堆化
siftDown(nums, i, 0);

View File

@@ -15,7 +15,7 @@ class QuickSort {
/* 哨兵划分 */
partition(nums: number[], left: number, right: number): number {
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
let i = left,
j = right;
while (i < j) {
@@ -86,7 +86,7 @@ class QuickSortMedian {
);
// 将中位数交换至数组最左端
this.swap(nums, left, med);
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
let i = left,
j = right;
while (i < j) {
@@ -127,7 +127,7 @@ class QuickSortTailCall {
/* 哨兵划分 */
partition(nums: number[], left: number, right: number): number {
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
let i = left,
j = right;
while (i < j) {
@@ -149,7 +149,7 @@ class QuickSortTailCall {
while (left < right) {
// 哨兵划分操作
let pivot = this.partition(nums, left, right);
// 对两个子数组中较短的那个执行快
// 对两个子数组中较短的那个执行快速排序
if (pivot - left < right - pivot) {
this.quickSort(nums, left, pivot - 1); // 递归排序左子数组
left = pivot + 1; // 剩余未排序区间为 [pivot + 1, right]

View File

@@ -12,7 +12,7 @@ function digit(num: number, exp: number): number {
/* 计数排序(根据 nums 第 k 位排序) */
function countingSortDigit(nums: number[], exp: number): void {
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶数组
const counter = new Array(10).fill(0);
const n = nums.length;
// 统计 0~9 各数字的出现次数

View File

@@ -32,7 +32,7 @@ class LinkedListDeque {
/* 队尾入队操作 */
pushLast(val: number): void {
const node: ListNode = new ListNode(val);
// 若链表为空,则令 front, rear 都指向 node
// 若链表为空,则令 front rear 都指向 node
if (this.queSize === 0) {
this.front = node;
this.rear = node;
@@ -48,7 +48,7 @@ class LinkedListDeque {
/* 队首入队操作 */
pushFirst(val: number): void {
const node: ListNode = new ListNode(val);
// 若链表为空,则令 front, rear 都指向 node
// 若链表为空,则令 front rear 都指向 node
if (this.queSize === 0) {
this.front = node;
this.rear = node;

View File

@@ -42,7 +42,7 @@ class ArrayBinaryTree {
/* 获取索引为 i 节点的父节点的索引 */
parent(i: number): number {
return Math.floor((i - 1) / 2); // 向下
return Math.floor((i - 1) / 2); // 向下整
}
/* 层序遍历 */

View File

@@ -14,7 +14,7 @@ let n1 = new TreeNode(1),
n3 = new TreeNode(3),
n4 = new TreeNode(4),
n5 = new TreeNode(5);
// 构建引用指向(即指针)
// 构建节点之间的引用(指针)
n1.left = n2;
n1.right = n3;
n2.left = n4;