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

@@ -37,11 +37,11 @@ void insert(int *nums, int size, int num, int index) {
for (int i = size - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// 将 num 赋给 index 处元素
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
/* 删除索引 index 处元素 */
// 注意stdio.h 占用了 remove 关键词
void removeItem(int *nums, int size, int index) {
// 把索引 index 之后的所有元素向前移动一位

View File

@@ -56,7 +56,7 @@ int main() {
ListNode *n2 = newListNode(2);
ListNode *n3 = newListNode(5);
ListNode *n4 = newListNode(4);
// 构建引用指向
// 构建节点之间的引用
n0->next = n1;
n1->next = n2;
n2->next = n3;

View File

@@ -6,7 +6,7 @@
#include "../utils/common.h"
/* 列表类简易实现 */
/* 列表类 */
typedef struct {
int *arr; // 数组(存储列表元素)
int capacity; // 列表容量
@@ -54,7 +54,7 @@ void set(MyList *nums, int index, int num) {
nums->arr[index] = num;
}
/* 尾部添加元素 */
/* 尾部添加元素 */
void add(MyList *nums, int num) {
if (size(nums) == capacity(nums)) {
extendCapacity(nums); // 扩容
@@ -63,7 +63,7 @@ void add(MyList *nums, int num) {
nums->size++;
}
/* 中间插入元素 */
/* 中间插入元素 */
void insert(MyList *nums, int index, int num) {
assert(index >= 0 && index < size(nums));
// 元素数量超出容量时,触发扩容机制
@@ -117,7 +117,7 @@ int *toArray(MyList *nums) {
int main() {
/* 初始化列表 */
MyList *nums = newMyList();
/* 尾部添加元素 */
/* 尾部添加元素 */
add(nums, 1);
add(nums, 3);
add(nums, 2);
@@ -127,7 +127,7 @@ int main() {
printArray(toArray(nums), size(nums));
printf("容量 = %d ,长度 = %d\n", capacity(nums), size(nums));
/* 中间插入元素 */
/* 中间插入元素 */
insert(nums, 3, 6);
printf("在索引 3 处插入数字 6 ,得到 nums = ");
printArray(toArray(nums), size(nums));

View File

@@ -26,7 +26,7 @@ void backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int
// 计算该格子对应的主对角线和副对角线
int diag1 = row - col + n - 1;
int diag2 = row + col;
// 剪枝:不允许该格子所在列、主对角线、副对角线存在皇后
// 剪枝:不允许该格子所在列、主对角线、副对角线存在皇后
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state[row][col] = 'Q';
@@ -51,8 +51,8 @@ char ***nQueens(int n, int *returnSize) {
state[i][n] = '\0';
}
bool cols[MAX_SIZE] = {false}; // 记录列是否有皇后
bool diags1[2 * MAX_SIZE - 1] = {false}; // 记录主对角线是否有皇后
bool diags2[2 * MAX_SIZE - 1] = {false}; // 记录副对角线是否有皇后
bool diags1[2 * MAX_SIZE - 1] = {false}; // 记录主对角线是否有皇后
bool diags2[2 * MAX_SIZE - 1] = {false}; // 记录副对角线是否有皇后
char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);
*returnSize = 0;

View File

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

View File

@@ -20,7 +20,7 @@ void move(int *src, int *srcSize, int *tar, int *tarSize) {
(*tarSize)++;
}
/* 求解汉诺塔问题 f(i) */
/* 求解汉诺塔问题 f(i) */
void dfs(int i, int *src, int *srcSize, int *buf, int *bufSize, int *tar, int *tarSize) {
// 若 src 只剩下一个圆盘,则直接将其移到 tar
if (i == 1) {
@@ -35,7 +35,7 @@ void dfs(int i, int *src, int *srcSize, int *buf, int *bufSize, int *tar, int *t
dfs(i - 1, buf, bufSize, src, srcSize, tar, tarSize);
}
/* 求解汉诺塔 */
/* 求解汉诺塔问题 */
void solveHanota(int *A, int *ASize, int *B, int *BSize, int *C, int *CSize) {
// 将 A 顶部 n 个圆盘借助 B 移到 C
dfs(*ASize, A, ASize, B, BSize, C, CSize);

View File

@@ -25,7 +25,7 @@ void backtrack(int *choices, int state, int n, int *res, int len) {
/* 爬楼梯:回溯 */
int climbingStairsBacktrack(int n) {
int choices[2] = {1, 2}; // 可选择向上爬 1 或 2 阶
int choices[2] = {1, 2}; // 可选择向上爬 1 或 2 阶
int state = 0; // 从第 0 阶开始爬
int *res = (int *)malloc(sizeof(int));
*res = 0; // 使用 res[0] 记录方案数量

View File

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

View File

@@ -72,7 +72,7 @@ int editDistanceDP(char *s, char *t, int n, int m) {
for (int j = 1; j <= m; j++) {
dp[0][j] = j;
}
// 状态转移:其余行列
// 状态转移:其余行
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i - 1] == t[j - 1]) {

View File

@@ -13,11 +13,11 @@ int myMax(int a, int b) {
/* 0-1 背包:暴力搜索 */
int knapsackDFS(int wgt[], int val[], int i, int c) {
// 若已选完所有物品或背包无容量,则返回价值 0
// 若已选完所有物品或背包无剩余容量,则返回价值 0
if (i == 0 || c == 0) {
return 0;
}
// 若超过背包容量,则只能不放入背包
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFS(wgt, val, i - 1, c);
}
@@ -30,7 +30,7 @@ int knapsackDFS(int wgt[], int val[], int i, int c) {
/* 0-1 背包:记忆化搜索 */
int knapsackDFSMem(int wgt[], int val[], int memCols, int **mem, int i, int c) {
// 若已选完所有物品或背包无容量,则返回价值 0
// 若已选完所有物品或背包无剩余容量,则返回价值 0
if (i == 0 || c == 0) {
return 0;
}
@@ -38,7 +38,7 @@ int knapsackDFSMem(int wgt[], int val[], int memCols, int **mem, int i, int c) {
if (mem[i][c] != -1) {
return mem[i][c];
}
// 若超过背包容量,则只能不放入背包
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);
}

View File

@@ -69,7 +69,7 @@ int minPathSumDP(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {
for (int i = 1; i < n; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
// 状态转移:其余行列
// 状态转移:其余行
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
dp[i][j] = myMin(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];

View File

@@ -66,7 +66,7 @@ void graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize,
// 遍历该顶点的所有邻接顶点
AdjListNode *node = findNode(graph, vet);
while (node != NULL) {
// 跳过已被访问的顶点
// 跳过已被访问的顶点
if (!isVisited(visited, *visitedSize, node->vertex)) {
enqueue(queue, node->vertex); // 只入队未访问的顶点
visited[(*visitedSize)++] = node->vertex; // 标记该顶点已被访问

View File

@@ -27,7 +27,7 @@ void dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {
// 遍历该顶点的所有邻接顶点
AdjListNode *node = findNode(graph, vet);
while (node != NULL) {
// 跳过已被访问的顶点
// 跳过已被访问的顶点
if (!isVisited(res, *resSize, node->vertex)) {
// 递归访问邻接顶点
dfs(graph, res, resSize, node->vertex);

View File

@@ -21,7 +21,7 @@ typedef struct {
int len;
} MapSet;
/* 基于数组简易实现的哈希表 */
/* 基于数组实现的哈希表 */
typedef struct {
Pair *buckets[HASHTABLE_CAPACITY];
} ArrayHashMap;

View File

@@ -99,7 +99,7 @@ int pop(MaxHeap *maxHeap) {
printf("heap is empty!");
return INT_MAX;
}
// 交换根节点与最右叶节点(交换首元素与尾元素)
// 交换根节点与最右叶节点(交换首元素与尾元素)
swap(maxHeap, 0, size(maxHeap) - 1);
// 删除节点
int val = maxHeap->data[maxHeap->size - 1];

View File

@@ -24,9 +24,9 @@ int binarySearch(int *nums, int len, int target) {
return -1;
}
/* 二分查找(左闭右开) */
/* 二分查找(左闭右开区间 */
int binarySearchLCRO(int *nums, int len, int target) {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
// 初始化左闭右开区间 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = len;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j) {
@@ -51,7 +51,7 @@ int main() {
int index = binarySearch(nums, 10, target);
printf("目标元素 6 的索引 = %d\n", index);
/* 二分查找(左闭右开) */
/* 二分查找(左闭右开区间 */
index = binarySearchLCRO(nums, 10, target);
printf("目标元素 6 的索引 = %d\n", index);

View File

@@ -34,7 +34,7 @@ void bucketSort(float nums[], int size) {
// 1. 将数组元素分配到各个桶中
for (int i = 0; i < size; i++) {
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
int bucket_idx = nums[i] * k;
int j = 0;
// 如果桶中有数据且数据小于当前值 nums[i], 要将其放到当前桶的后面,相当于 cpp 中的 push_back

View File

@@ -38,7 +38,7 @@ void heapSort(int nums[], int n) {
}
// 从堆中提取最大元素,循环 n-1 轮
for (int i = n - 1; i > 0; --i) {
// 交换根节点与最右叶节点(交换首元素与尾元素)
// 交换根节点与最右叶节点(交换首元素与尾元素)
int tmp = nums[0];
nums[0] = nums[i];
nums[i] = tmp;

View File

@@ -16,7 +16,7 @@ void swap(int nums[], int i, int j) {
/* 快速排序类 */
// 快速排序类-哨兵划分
int partition(int nums[], int left, int right) {
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left]) {
@@ -68,7 +68,7 @@ int partitionMedian(int nums[], int left, int right) {
int med = medianThree(nums, left, (left + right) / 2, right);
// 将中位数交换至数组最左端
swap(nums, left, med);
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
@@ -100,7 +100,7 @@ void quickSortTailCall(int nums[], int left, int right) {
while (left < right) {
// 哨兵划分操作
int pivot = partition(nums, left, right);
// 对两个子数组中较短的那个执行快
// 对两个子数组中较短的那个执行快速排序
if (pivot - left < right - pivot) {
quickSortTailCall(nums, left, pivot - 1); // 递归排序左子数组
left = pivot + 1; // 剩余未排序区间为 [pivot + 1, right]

View File

@@ -14,7 +14,7 @@ int digit(int num, int exp) {
/* 计数排序(根据 nums 第 k 位排序) */
void countingSortDigit(int nums[], int size, int exp) {
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶数组
int *counter = (int *)malloc((sizeof(int) * 10));
// 统计 0~9 各数字的出现次数
for (int i = 0; i < size; i++) {

View File

@@ -67,7 +67,7 @@ bool empty(LinkedListDeque *deque) {
/* 入队 */
void push(LinkedListDeque *deque, int num, bool isFront) {
DoublyListNode *node = newDoublyListNode(num);
// 若链表为空,则令 front, rear 都指向node
// 若链表为空,则令 front rear 都指向node
if (empty(deque)) {
deque->front = deque->rear = node;
}

View File

@@ -15,7 +15,7 @@ int main() {
TreeNode *n3 = newTreeNode(3);
TreeNode *n4 = newTreeNode(4);
TreeNode *n5 = newTreeNode(5);
// 构建引用指向(即指针)
// 构建节点之间的引用(指针)
n1->left = n2;
n1->right = n3;
n2->left = n4;

View File

@@ -35,11 +35,11 @@ void insert(int *nums, int size, int num, int index) {
for (int i = size - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// 将 num 赋给 index 处元素
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
/* 删除索引 index 处元素 */
void remove(int *nums, int size, int index) {
// 把索引 index 之后的所有元素向前移动一位
for (int i = index; i < size - 1; i++) {

View File

@@ -56,7 +56,7 @@ int main() {
ListNode *n2 = new ListNode(2);
ListNode *n3 = new ListNode(5);
ListNode *n4 = new ListNode(4);
// 构建引用指向
// 构建节点之间的引用
n0->next = n1;
n1->next = n2;
n2->next = n3;

View File

@@ -27,7 +27,7 @@ int main() {
cout << "清空列表后 nums = ";
printVector(nums);
/* 尾部添加元素 */
/* 尾部添加元素 */
nums.push_back(1);
nums.push_back(3);
nums.push_back(2);
@@ -36,7 +36,7 @@ int main() {
cout << "添加元素后 nums = ";
printVector(nums);
/* 中间插入元素 */
/* 中间插入元素 */
nums.insert(nums.begin() + 3, 6);
cout << "在索引 3 处插入数字 6 ,得到 nums = ";
printVector(nums);

View File

@@ -6,12 +6,12 @@
#include "../utils/common.hpp"
/* 列表类简易实现 */
/* 列表类 */
class MyList {
private:
int *arr; // 数组(存储列表元素)
int arrCapacity = 10; // 列表容量
int arrSize = 0; // 列表长度(当前元素数量)
int arrSize = 0; // 列表长度(当前元素数量)
int extendRatio = 2; // 每次列表扩容的倍数
public:
@@ -25,7 +25,7 @@ class MyList {
delete[] arr;
}
/* 获取列表长度(当前元素数量)*/
/* 获取列表长度(当前元素数量)*/
int size() {
return arrSize;
}
@@ -50,7 +50,7 @@ class MyList {
arr[index] = num;
}
/* 尾部添加元素 */
/* 尾部添加元素 */
void add(int num) {
// 元素数量超出容量时,触发扩容机制
if (size() == capacity())
@@ -60,7 +60,7 @@ class MyList {
arrSize++;
}
/* 中间插入元素 */
/* 中间插入元素 */
void insert(int index, int num) {
if (index < 0 || index >= size())
throw out_of_range("索引越界");
@@ -121,7 +121,7 @@ class MyList {
int main() {
/* 初始化列表 */
MyList *nums = new MyList();
/* 尾部添加元素 */
/* 尾部添加元素 */
nums->add(1);
nums->add(3);
nums->add(2);
@@ -132,7 +132,7 @@ int main() {
printVector(vec);
cout << "容量 = " << nums->capacity() << " ,长度 = " << nums->size() << endl;
/* 中间插入元素 */
/* 中间插入元素 */
nums->insert(3, 6);
cout << "在索引 3 处插入数字 6 ,得到 nums = ";
vec = nums->toVector();

View File

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

View File

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

View File

@@ -15,7 +15,7 @@ void move(vector<int> &src, vector<int> &tar) {
tar.push_back(pan);
}
/* 求解汉诺塔问题 f(i) */
/* 求解汉诺塔问题 f(i) */
void dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {
// 若 src 只剩下一个圆盘,则直接将其移到 tar
if (i == 1) {
@@ -30,7 +30,7 @@ void dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {
dfs(i - 1, buf, src, tar);
}
/* 求解汉诺塔 */
/* 求解汉诺塔问题 */
void solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {
int n = A.size();
// 将 A 顶部 n 个圆盘借助 B 移到 C

View File

@@ -25,7 +25,7 @@ void backtrack(vector<int> &choices, int state, int n, vector<int> &res) {
/* 爬楼梯:回溯 */
int climbingStairsBacktrack(int n) {
vector<int> choices = {1, 2}; // 可选择向上爬 1 或 2 阶
vector<int> choices = {1, 2}; // 可选择向上爬 1 或 2 阶
int state = 0; // 从第 0 阶开始爬
vector<int> res = {0}; // 使用 res[0] 记录方案数量
backtrack(choices, state, n, res);

View File

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

View File

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

View File

@@ -6,11 +6,11 @@ using namespace std;
/* 0-1 背包:暴力搜索 */
int knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {
// 若已选完所有物品或背包无容量,则返回价值 0
// 若已选完所有物品或背包无剩余容量,则返回价值 0
if (i == 0 || c == 0) {
return 0;
}
// 若超过背包容量,则只能不放入背包
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFS(wgt, val, i - 1, c);
}
@@ -23,7 +23,7 @@ int knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {
/* 0-1 背包:记忆化搜索 */
int knapsackDFSMem(vector<int> &wgt, vector<int> &val, vector<vector<int>> &mem, int i, int c) {
// 若已选完所有物品或背包无容量,则返回价值 0
// 若已选完所有物品或背包无剩余容量,则返回价值 0
if (i == 0 || c == 0) {
return 0;
}
@@ -31,7 +31,7 @@ int knapsackDFSMem(vector<int> &wgt, vector<int> &val, vector<vector<int>> &mem,
if (mem[i][c] != -1) {
return mem[i][c];
}
// 若超过背包容量,则只能不放入背包
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFSMem(wgt, val, mem, i - 1, c);
}

View File

@@ -59,7 +59,7 @@ int minPathSumDP(vector<vector<int>> &grid) {
for (int i = 1; i < n; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
// 状态转移:其余行列
// 状态转移:其余行
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];

View File

@@ -9,7 +9,7 @@
/* 基于邻接表实现的无向图类 */
class GraphAdjList {
public:
// 邻接表key: 顶点value该顶点的所有邻接顶点
// 邻接表key顶点value该顶点的所有邻接顶点
unordered_map<Vertex *, vector<Vertex *>> adjList;
/* 在 vector 中删除指定节点 */

View File

@@ -65,7 +65,7 @@ class GraphAdjMat {
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {
throw out_of_range("顶点不存在");
}
// 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i)
// 在无向图中,邻接矩阵关于主对角线对称,即满足 (i, j) == (j, i)
adjMat[i][j] = 1;
adjMat[j][i] = 1;
}

View File

@@ -25,7 +25,7 @@ vector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {
// 遍历该顶点的所有邻接顶点
for (auto adjVet : graph.adjList[vet]) {
if (visited.count(adjVet))
continue; // 跳过已被访问的顶点
continue; // 跳过已被访问的顶点
que.push(adjVet); // 只入队未访问的顶点
visited.emplace(adjVet); // 标记该顶点已被访问
}

View File

@@ -14,7 +14,7 @@ void dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *>
// 遍历该顶点的所有邻接顶点
for (Vertex *adjVet : graph.adjList[vet]) {
if (visited.count(adjVet))
continue; // 跳过已被访问的顶点
continue; // 跳过已被访问的顶点
// 递归访问邻接顶点
dfs(graph, visited, res, adjVet);
}

View File

@@ -17,7 +17,7 @@ struct Pair {
}
};
/* 基于数组简易实现的哈希表 */
/* 基于数组实现的哈希表 */
class ArrayHashMap {
private:
vector<Pair *> buckets;

View File

@@ -24,7 +24,7 @@ class MaxHeap {
/* 获取父节点索引 */
int parent(int i) {
return (i - 1) / 2; // 向下
return (i - 1) / 2; // 向下整
}
/* 从节点 i 开始,从底至顶堆化 */
@@ -100,7 +100,7 @@ class MaxHeap {
if (isEmpty()) {
throw out_of_range("堆为空");
}
// 交换根节点与最右叶节点(交换首元素与尾元素)
// 交换根节点与最右叶节点(交换首元素与尾元素)
swap(maxHeap[0], maxHeap[size() - 1]);
// 删除节点
maxHeap.pop_back();

View File

@@ -24,9 +24,9 @@ int binarySearch(vector<int> &nums, int target) {
return -1;
}
/* 二分查找(左闭右开) */
/* 二分查找(左闭右开区间 */
int binarySearchLCRO(vector<int> &nums, int target) {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
// 初始化左闭右开区间 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = nums.size();
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j) {
@@ -51,7 +51,7 @@ int main() {
int index = binarySearch(nums, target);
cout << "目标元素 6 的索引 = " << index << endl;
/* 二分查找(左闭右开) */
/* 二分查找(左闭右开区间 */
index = binarySearchLCRO(nums, target);
cout << "目标元素 6 的索引 = " << index << endl;

View File

@@ -9,7 +9,7 @@
/* 方法一:暴力枚举 */
vector<int> twoSumBruteForce(vector<int> &nums, int target) {
int size = nums.size();
// 两层循环,时间复杂度 O(n^2)
// 两层循环,时间复杂度 O(n^2)
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target)
@@ -22,9 +22,9 @@ vector<int> twoSumBruteForce(vector<int> &nums, int target) {
/* 方法二:辅助哈希表 */
vector<int> twoSumHashTable(vector<int> &nums, int target) {
int size = nums.size();
// 辅助哈希表,空间复杂度 O(n)
// 辅助哈希表,空间复杂度 O(n)
unordered_map<int, int> dic;
// 单层循环,时间复杂度 O(n)
// 单层循环,时间复杂度 O(n)
for (int i = 0; i < size; i++) {
if (dic.find(target - nums[i]) != dic.end()) {
return {dic[target - nums[i]], i};

View File

@@ -13,7 +13,7 @@ void bucketSort(vector<float> &nums) {
vector<vector<float>> buckets(k);
// 1. 将数组元素分配到各个桶中
for (float num : nums) {
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
int i = num * k;
// 将 num 添加进桶 bucket_idx
buckets[i].push_back(num);

View File

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

View File

@@ -18,7 +18,7 @@ class QuickSort {
/* 哨兵划分 */
static int partition(vector<int> &nums, int left, int right) {
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
@@ -73,7 +73,7 @@ class QuickSortMedian {
int med = medianThree(nums, left, (left + right) / 2, right);
// 将中位数交换至数组最左端
swap(nums, left, med);
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
@@ -112,7 +112,7 @@ class QuickSortTailCall {
/* 哨兵划分 */
static int partition(vector<int> &nums, int left, int right) {
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
@@ -132,7 +132,7 @@ class QuickSortTailCall {
while (left < right) {
// 哨兵划分操作
int pivot = partition(nums, left, right);
// 对两个子数组中较短的那个执行快
// 对两个子数组中较短的那个执行快速排序
if (pivot - left < right - pivot) {
quickSort(nums, left, pivot - 1); // 递归排序左子数组
left = pivot + 1; // 剩余未排序区间为 [pivot + 1, right]

View File

@@ -14,7 +14,7 @@ int digit(int num, int exp) {
/* 计数排序(根据 nums 第 k 位排序) */
void countingSortDigit(vector<int> &nums, int exp) {
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶数组
vector<int> counter(10, 0);
int n = nums.size();
// 统计 0~9 各数字的出现次数

View File

@@ -50,7 +50,7 @@ class LinkedListDeque {
/* 入队操作 */
void push(int num, bool isFront) {
DoublyListNode *node = new DoublyListNode(num);
// 若链表为空,则令 front, rear 都指向 node
// 若链表为空,则令 front rear 都指向 node
if (isEmpty())
front = rear = node;
// 队首入队操作

View File

@@ -15,7 +15,7 @@ int main() {
TreeNode *n3 = new TreeNode(3);
TreeNode *n4 = new TreeNode(4);
TreeNode *n5 = new TreeNode(5);
// 构建引用指向(即指针)
// 构建节点之间的引用(指针)
n1->left = n2;
n1->right = n3;
n2->left = n4;

View File

@@ -33,11 +33,11 @@ public class array {
for (int i = nums.Length - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// 将 num 赋给 index 处元素
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
/* 删除索引 index 处元素 */
void Remove(int[] nums, int index) {
// 把索引 index 之后的所有元素向前移动一位
for (int i = index; i < nums.Length - 1; i++) {

View File

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

View File

@@ -27,7 +27,7 @@ public class list {
nums.Clear();
Console.WriteLine("清空列表后 nums = " + string.Join(",", nums));
/* 尾部添加元素 */
/* 尾部添加元素 */
nums.Add(1);
nums.Add(3);
nums.Add(2);
@@ -35,7 +35,7 @@ public class list {
nums.Add(4);
Console.WriteLine("添加元素后 nums = " + string.Join(",", nums));
/* 中间插入元素 */
/* 中间插入元素 */
nums.Insert(3, 6);
Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + string.Join(",", nums));

View File

@@ -6,11 +6,11 @@
namespace hello_algo.chapter_array_and_linkedlist;
/* 列表类简易实现 */
/* 列表类 */
class MyList {
private int[] arr; // 数组(存储列表元素)
private int arrCapacity = 10; // 列表容量
private int arrSize = 0; // 列表长度(当前元素数量)
private int arrSize = 0; // 列表长度(当前元素数量)
private readonly int extendRatio = 2; // 每次列表扩容的倍数
/* 构造方法 */
@@ -18,7 +18,7 @@ class MyList {
arr = new int[arrCapacity];
}
/* 获取列表长度(当前元素数量)*/
/* 获取列表长度(当前元素数量)*/
public int Size() {
return arrSize;
}
@@ -43,7 +43,7 @@ class MyList {
arr[index] = num;
}
/* 尾部添加元素 */
/* 尾部添加元素 */
public void Add(int num) {
// 元素数量超出容量时,触发扩容机制
if (arrSize == arrCapacity)
@@ -53,7 +53,7 @@ class MyList {
arrSize++;
}
/* 中间插入元素 */
/* 中间插入元素 */
public void Insert(int index, int num) {
if (index < 0 || index >= arrSize)
throw new IndexOutOfRangeException("索引越界");
@@ -108,7 +108,7 @@ public class my_list {
public void Test() {
/* 初始化列表 */
MyList nums = new();
/* 尾部添加元素 */
/* 尾部添加元素 */
nums.Add(1);
nums.Add(3);
nums.Add(2);
@@ -117,7 +117,7 @@ public class my_list {
Console.WriteLine("列表 nums = " + string.Join(",", nums.ToArray()) +
" ,容量 = " + nums.Capacity() + " ,长度 = " + nums.Size());
/* 中间插入元素 */
/* 中间插入元素 */
nums.Insert(3, 6);
Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + string.Join(",", nums.ToArray()));

View File

@@ -24,7 +24,7 @@ public class n_queens {
// 计算该格子对应的主对角线和副对角线
int diag1 = row - col + n - 1;
int diag2 = row + col;
// 剪枝:不允许该格子所在列、主对角线、副对角线存在皇后
// 剪枝:不允许该格子所在列、主对角线、副对角线存在皇后
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state[row][col] = "Q";
@@ -50,8 +50,8 @@ public class n_queens {
state.Add(row);
}
bool[] cols = new bool[n]; // 记录列是否有皇后
bool[] diags1 = new bool[2 * n - 1]; // 记录主对角线是否有皇后
bool[] diags2 = new bool[2 * n - 1]; // 记录副对角线是否有皇后
bool[] diags1 = new bool[2 * n - 1]; // 记录主对角线是否有皇后
bool[] diags2 = new bool[2 * n - 1]; // 记录副对角线是否有皇后
List<List<List<string>>> res = [];
Backtrack(0, n, state, res, cols, diags1, diags2);

View File

@@ -16,7 +16,7 @@ public class hanota {
tar.Add(pan);
}
/* 求解汉诺塔问题 f(i) */
/* 求解汉诺塔问题 f(i) */
void DFS(int i, List<int> src, List<int> buf, List<int> tar) {
// 若 src 只剩下一个圆盘,则直接将其移到 tar
if (i == 1) {
@@ -31,7 +31,7 @@ public class hanota {
DFS(i - 1, buf, src, tar);
}
/* 求解汉诺塔 */
/* 求解汉诺塔问题 */
void SolveHanota(List<int> A, List<int> B, List<int> C) {
int n = A.Count;
// 将 A 顶部 n 个圆盘借助 B 移到 C

View File

@@ -25,7 +25,7 @@ public class climbing_stairs_backtrack {
/* 爬楼梯:回溯 */
int ClimbingStairsBacktrack(int n) {
List<int> choices = [1, 2]; // 可选择向上爬 1 或 2 阶
List<int> choices = [1, 2]; // 可选择向上爬 1 或 2 阶
int state = 0; // 从第 0 阶开始爬
List<int> res = [0]; // 使用 res[0] 记录方案数量
Backtrack(choices, state, n, res);

View File

@@ -17,7 +17,7 @@ public class coin_change {
for (int a = 1; a <= amt; a++) {
dp[0, a] = MAX;
}
// 状态转移:其余行列
// 状态转移:其余行
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {

View File

@@ -66,7 +66,7 @@ public class edit_distance {
for (int j = 1; j <= m; j++) {
dp[0, j] = j;
}
// 状态转移:其余行列
// 状态转移:其余行
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i - 1] == t[j - 1]) {

View File

@@ -9,11 +9,11 @@ namespace hello_algo.chapter_dynamic_programming;
public class knapsack {
/* 0-1 背包:暴力搜索 */
int KnapsackDFS(int[] weight, int[] val, int i, int c) {
// 若已选完所有物品或背包无容量,则返回价值 0
// 若已选完所有物品或背包无剩余容量,则返回价值 0
if (i == 0 || c == 0) {
return 0;
}
// 若超过背包容量,则只能不放入背包
// 若超过背包容量,则只能选择不放入背包
if (weight[i - 1] > c) {
return KnapsackDFS(weight, val, i - 1, c);
}
@@ -26,7 +26,7 @@ public class knapsack {
/* 0-1 背包:记忆化搜索 */
int KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {
// 若已选完所有物品或背包无容量,则返回价值 0
// 若已选完所有物品或背包无剩余容量,则返回价值 0
if (i == 0 || c == 0) {
return 0;
}
@@ -34,7 +34,7 @@ public class knapsack {
if (mem[i][c] != -1) {
return mem[i][c];
}
// 若超过背包容量,则只能不放入背包
// 若超过背包容量,则只能选择不放入背包
if (weight[i - 1] > c) {
return KnapsackDFSMem(weight, val, mem, i - 1, c);
}

View File

@@ -60,7 +60,7 @@ public class min_path_sum {
for (int i = 1; i < n; i++) {
dp[i, 0] = dp[i - 1, 0] + grid[i][0];
}
// 状态转移:其余行列
// 状态转移:其余行
for (int i = 1; i < n; i++) {
for (int j = 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 @@ namespace hello_algo.chapter_graph;
/* 基于邻接表实现的无向图类 */
public class GraphAdjList {
// 邻接表key: 顶点value该顶点的所有邻接顶点
// 邻接表key顶点value该顶点的所有邻接顶点
public Dictionary<Vertex, List<Vertex>> adjList;
/* 构造函数 */

View File

@@ -68,7 +68,7 @@ class GraphAdjMat {
// 索引越界与相等处理
if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)
throw new IndexOutOfRangeException();
// 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i)
// 在无向图中,邻接矩阵关于主对角线对称,即满足 (i, j) == (j, i)
adjMat[i][j] = 1;
adjMat[j][i] = 1;
}

View File

@@ -23,7 +23,7 @@ public class graph_bfs {
res.Add(vet); // 记录访问顶点
foreach (Vertex adjVet in graph.adjList[vet]) {
if (visited.Contains(adjVet)) {
continue; // 跳过已被访问的顶点
continue; // 跳过已被访问的顶点
}
que.Enqueue(adjVet); // 只入队未访问的顶点
visited.Add(adjVet); // 标记该顶点已被访问

View File

@@ -14,7 +14,7 @@ public class graph_dfs {
// 遍历该顶点的所有邻接顶点
foreach (Vertex adjVet in graph.adjList[vet]) {
if (visited.Contains(adjVet)) {
continue; // 跳过已被访问的顶点
continue; // 跳过已被访问的顶点
}
// 递归访问邻接顶点
DFS(graph, visited, res, adjVet);

View File

@@ -12,7 +12,7 @@ class Pair(int key, string val) {
public string val = val;
}
/* 基于数组简易实现的哈希表 */
/* 基于数组实现的哈希表 */
class ArrayHashMap {
List<Pair?> buckets;
public ArrayHashMap() {

View File

@@ -85,7 +85,7 @@ class MaxHeap {
// 判空处理
if (IsEmpty())
throw new IndexOutOfRangeException();
// 交换根节点与最右叶节点(交换首元素与尾元素)
// 交换根节点与最右叶节点(交换首元素与尾元素)
Swap(0, Size() - 1);
// 删除节点
int val = maxHeap.Last();

View File

@@ -25,9 +25,9 @@ public class binary_search {
return -1;
}
/* 二分查找(左闭右开) */
/* 二分查找(左闭右开区间 */
int BinarySearchLCRO(int[] nums, int target) {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
// 初始化左闭右开区间 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = nums.Length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j) {
@@ -52,7 +52,7 @@ public class binary_search {
int index = BinarySearch(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
/* 二分查找(左闭右开) */
/* 二分查找(左闭右开区间 */
index = BinarySearchLCRO(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
}

View File

@@ -10,7 +10,7 @@ public class two_sum {
/* 方法一:暴力枚举 */
int[] TwoSumBruteForce(int[] nums, int target) {
int size = nums.Length;
// 两层循环,时间复杂度 O(n^2)
// 两层循环,时间复杂度 O(n^2)
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target)
@@ -23,9 +23,9 @@ public class two_sum {
/* 方法二:辅助哈希表 */
int[] TwoSumHashTable(int[] nums, int target) {
int size = nums.Length;
// 辅助哈希表,空间复杂度 O(n)
// 辅助哈希表,空间复杂度 O(n)
Dictionary<int, int> dic = [];
// 单层循环,时间复杂度 O(n)
// 单层循环,时间复杂度 O(n)
for (int i = 0; i < size; i++) {
if (dic.ContainsKey(target - nums[i])) {
return [dic[target - nums[i]], i];

View File

@@ -17,7 +17,7 @@ public class bucket_sort {
}
// 1. 将数组元素分配到各个桶中
foreach (float num in nums) {
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
int i = (int)(num * k);
// 将 num 添加进桶 i
buckets[i].Add(num);

View File

@@ -36,7 +36,7 @@ public class heap_sort {
}
// 从堆中提取最大元素,循环 n-1 轮
for (int i = nums.Length - 1; i > 0; i--) {
// 交换根节点与最右叶节点(交换首元素与尾元素)
// 交换根节点与最右叶节点(交换首元素与尾元素)
(nums[i], nums[0]) = (nums[0], nums[i]);
// 以根节点为起点,从顶至底进行堆化
SiftDown(nums, i, 0);

View File

@@ -14,7 +14,7 @@ class quickSort {
/* 哨兵划分 */
static int Partition(int[] nums, int left, int right) {
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
@@ -65,7 +65,7 @@ class QuickSortMedian {
int med = MedianThree(nums, left, (left + right) / 2, right);
// 将中位数交换至数组最左端
Swap(nums, left, med);
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
@@ -100,7 +100,7 @@ class QuickSortTailCall {
/* 哨兵划分 */
static int Partition(int[] nums, int left, int right) {
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
@@ -119,7 +119,7 @@ class QuickSortTailCall {
while (left < right) {
// 哨兵划分操作
int pivot = Partition(nums, left, right);
// 对两个子数组中较短的那个执行快
// 对两个子数组中较短的那个执行快速排序
if (pivot - left < right - pivot) {
QuickSort(nums, left, pivot - 1); // 递归排序左子数组
left = pivot + 1; // 剩余未排序区间为 [pivot + 1, right]

View File

@@ -15,7 +15,7 @@ public class radix_sort {
/* 计数排序(根据 nums 第 k 位排序) */
void CountingSortDigit(int[] nums, int exp) {
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶数组
int[] counter = new int[10];
int n = nums.Length;
// 统计 0~9 各数字的出现次数

View File

@@ -36,7 +36,7 @@ public class LinkedListDeque {
/* 入队操作 */
void Push(int num, bool isFront) {
ListNode node = new(num);
// 若链表为空,则令 front, rear 都指向 node
// 若链表为空,则令 front rear 都指向 node
if (IsEmpty()) {
front = node;
rear = node;

View File

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

View File

@@ -39,7 +39,7 @@ void insert(List<int> nums, int _num, int index) {
nums[index] = _num;
}
/* 删除索引 index 处元素 */
/* 删除索引 index 处元素 */
void remove(List<int> nums, int index) {
// 把索引 index 之后的所有元素向前移动一位
for (var i = index; i < nums.length - 1; i++) {

View File

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

View File

@@ -24,7 +24,7 @@ void main() {
nums.clear();
print('清空列表后 nums = $nums');
/* 尾部添加元素 */
/* 尾部添加元素 */
nums.add(1);
nums.add(3);
nums.add(2);
@@ -32,7 +32,7 @@ void main() {
nums.add(4);
print('添加元素后 nums = $nums');
/* 中间插入元素 */
/* 中间插入元素 */
nums.insert(3, 6);
print('在索引 3 处插入数字 6 ,得到 nums = $nums');

View File

@@ -4,11 +4,11 @@
* Author: Jefferson (JeffersonHuang77@gmail.com)
*/
/* 列表类简易实现 */
/* 列表类 */
class MyList {
late List<int> _arr; // 数组(存储列表元素)
int _capacity = 10; // 列表容量
int _size = 0; // 列表长度(当前元素数量)
int _size = 0; // 列表长度(当前元素数量)
int _extendRatio = 2; // 每次列表扩容的倍数
/* 构造方法 */
@@ -16,7 +16,7 @@ class MyList {
_arr = List.filled(_capacity, 0);
}
/* 获取列表长度(当前元素数量)*/
/* 获取列表长度(当前元素数量)*/
int size() => _size;
/* 获取列表容量 */
@@ -34,7 +34,7 @@ class MyList {
_arr[index] = _num;
}
/* 尾部添加元素 */
/* 尾部添加元素 */
void add(int _num) {
// 元素数量超出容量时,触发扩容机制
if (_size == _capacity) extendCapacity();
@@ -43,7 +43,7 @@ class MyList {
_size++;
}
/* 中间插入元素 */
/* 中间插入元素 */
void insert(int index, int _num) {
if (index >= _size) throw RangeError('索引越界');
// 元素数量超出容量时,触发扩容机制
@@ -97,7 +97,7 @@ class MyList {
void main() {
/* 初始化列表 */
MyList nums = MyList();
/* 尾部添加元素 */
/* 尾部添加元素 */
nums.add(1);
nums.add(3);
nums.add(2);
@@ -106,7 +106,7 @@ void main() {
print(
'列表 nums = ${nums.toArray()} ,容量 = ${nums.capacity()} ,长度 = ${nums.size()}');
/* 中间插入元素 */
/* 中间插入元素 */
nums.insert(3, 6);
print('在索引 3 处插入数字 6 ,得到 nums = ${nums.toArray()}');

View File

@@ -28,7 +28,7 @@ void backtrack(
// 计算该格子对应的主对角线和副对角线
int diag1 = row - col + n - 1;
int diag2 = row + col;
// 剪枝:不允许该格子所在列、主对角线、副对角线存在皇后
// 剪枝:不允许该格子所在列、主对角线、副对角线存在皇后
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state[row][col] = "Q";
@@ -51,8 +51,8 @@ List<List<List<String>>> nQueens(int n) {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
List<List<String>> state = List.generate(n, (index) => List.filled(n, "#"));
List<bool> cols = List.filled(n, false); // 记录列是否有皇后
List<bool> diags1 = List.filled(2 * n - 1, false); // 记录主对角线是否有皇后
List<bool> diags2 = List.filled(2 * n - 1, false); // 记录副对角线是否有皇后
List<bool> diags1 = List.filled(2 * n - 1, false); // 记录主对角线是否有皇后
List<bool> diags2 = List.filled(2 * n - 1, false); // 记录副对角线是否有皇后
List<List<List<String>>> res = [];
backtrack(0, n, state, res, cols, diags1, diags2);

View File

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

View File

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

View File

@@ -22,7 +22,7 @@ void backtrack(List<int> choices, int state, int n, List<int> res) {
/* 爬楼梯:回溯 */
int climbingStairsBacktrack(int n) {
List<int> choices = [1, 2]; // 可选择向上爬 1 或 2 阶
List<int> choices = [1, 2]; // 可选择向上爬 1 或 2 阶
int state = 0; // 从第 0 阶开始爬
List<int> res = [];
res.add(0); // 使用 res[0] 记录方案数量

View File

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

View File

@@ -56,7 +56,7 @@ int editDistanceDP(String s, String t) {
for (int j = 1; j <= m; j++) {
dp[0][j] = j;
}
// 状态转移:其余行列
// 状态转移:其余行
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i - 1] == t[j - 1]) {

View File

@@ -8,11 +8,11 @@ import 'dart:math';
/* 0-1 背包:暴力搜索 */
int knapsackDFS(List<int> wgt, List<int> val, int i, int c) {
// 若已选完所有物品或背包无容量,则返回价值 0
// 若已选完所有物品或背包无剩余容量,则返回价值 0
if (i == 0 || c == 0) {
return 0;
}
// 若超过背包容量,则只能不放入背包
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFS(wgt, val, i - 1, c);
}
@@ -31,7 +31,7 @@ int knapsackDFSMem(
int i,
int c,
) {
// 若已选完所有物品或背包无容量,则返回价值 0
// 若已选完所有物品或背包无剩余容量,则返回价值 0
if (i == 0 || c == 0) {
return 0;
}
@@ -39,7 +39,7 @@ int 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

@@ -61,7 +61,7 @@ int minPathSumDP(List<List<int>> grid) {
for (int i = 1; i < n; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
// 状态转移:其余行列
// 状态转移:其余行
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];

View File

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

View File

@@ -67,7 +67,7 @@ class GraphAdjMat {
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {
throw IndexError;
}
// 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i)
// 在无向图中,邻接矩阵关于主对角线对称,即满足 (i, j) == (j, i)
adjMat[i][j] = 1;
adjMat[j][i] = 1;
}

View File

@@ -27,7 +27,7 @@ List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
// 遍历该顶点的所有邻接顶点
for (Vertex adjVet in graph.adjList[vet]!) {
if (visited.contains(adjVet)) {
continue; // 跳过已被访问的顶点
continue; // 跳过已被访问的顶点
}
que.add(adjVet); // 只入队未访问的顶点
visited.add(adjVet); // 标记该顶点已被访问

View File

@@ -19,7 +19,7 @@ void dfs(
// 遍历该顶点的所有邻接顶点
for (Vertex adjVet in graph.adjList[vet]!) {
if (visited.contains(adjVet)) {
continue; // 跳过已被访问的顶点
continue; // 跳过已被访问的顶点
}
// 递归访问邻接顶点
dfs(graph, visited, res, adjVet);

View File

@@ -11,7 +11,7 @@ class Pair {
Pair(this.key, this.val);
}
/* 基于数组简易实现的哈希表 */
/* 基于数组实现的哈希表 */
class ArrayHashMap {
late List<Pair?> _buckets;

View File

@@ -85,7 +85,7 @@ class MaxHeap {
int pop() {
// 判空处理
if (isEmpty()) throw Exception('堆为空');
// 交换根节点与最右叶节点(交换首元素与尾元素)
// 交换根节点与最右叶节点(交换首元素与尾元素)
_swap(0, size() - 1);
// 删除节点
int val = _maxHeap.removeLast();

View File

@@ -115,7 +115,7 @@ class MinHeap {
int pop() {
// 判空处理
if (isEmpty()) throw Exception('堆为空');
// 交换根节点与最右叶节点(交换首元素与尾元素)
// 交换根节点与最右叶节点(交换首元素与尾元素)
_swap(0, size() - 1);
// 删除节点
int val = _minHeap.removeLast();

View File

@@ -28,7 +28,7 @@ int binarySearch(List<int> nums, int target) {
/* 二分查找(左闭右开区间) */
int binarySearchLCRO(List<int> nums, int target) {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
// 初始化左闭右开区间 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = nums.length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j) {

View File

@@ -9,7 +9,7 @@ import 'dart:collection';
/* 方法一: 暴力枚举 */
List<int> twoSumBruteForce(List<int> nums, int target) {
int size = nums.length;
// 两层循环,时间复杂度 O(n^2)
// 两层循环,时间复杂度 O(n^2)
for (var i = 0; i < size - 1; i++) {
for (var j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target) return [i, j];
@@ -21,9 +21,9 @@ List<int> twoSumBruteForce(List<int> nums, int target) {
/* 方法二: 辅助哈希表 */
List<int> twoSumHashTable(List<int> nums, int target) {
int size = nums.length;
// 辅助哈希表,空间复杂度 O(n)
// 辅助哈希表,空间复杂度 O(n)
Map<int, int> dic = HashMap();
// 单层循环,时间复杂度 O(n)
// 单层循环,时间复杂度 O(n)
for (var i = 0; i < size; i++) {
if (dic.containsKey(target - nums[i])) {
return [dic[target - nums[i]]!, i];

View File

@@ -12,7 +12,7 @@ void bucketSort(List<double> nums) {
// 1. 将数组元素分配到各个桶中
for (double _num in nums) {
// 输入数据范围 [0, 1),使用 _num * k 映射到索引范围 [0, k-1]
// 输入数据范围 [0, 1),使用 _num * k 映射到索引范围 [0, k-1]
int i = (_num * k).toInt();
// 将 _num 添加进桶 bucket_idx
buckets[i].add(_num);

View File

@@ -32,7 +32,7 @@ void heapSort(List<int> nums) {
}
// 从堆中提取最大元素,循环 n-1 轮
for (int i = nums.length - 1; i > 0; i--) {
// 交换根节点与最右叶节点(交换首元素与尾元素)
// 交换根节点与最右叶节点(交换首元素与尾元素)
int tmp = nums[0];
nums[0] = nums[i];
nums[i] = tmp;

View File

@@ -15,7 +15,7 @@ class QuickSort {
/* 哨兵划分 */
static int _partition(List<int> nums, int left, int right) {
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left]) j--; // 从右向左找首个小于基准数的元素
@@ -65,7 +65,7 @@ class QuickSortMedian {
int med = _medianThree(nums, left, (left + right) ~/ 2, right);
// 将中位数交换至数组最左端
_swap(nums, left, med);
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left]) j--; // 从右向左找首个小于基准数的元素
@@ -99,7 +99,7 @@ class QuickSortTailCall {
/* 哨兵划分 */
static int _partition(List<int> nums, int left, int right) {
// 以 nums[left] 为基准数
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left]) j--; // 从右向左找首个小于基准数的元素
@@ -116,7 +116,7 @@ class QuickSortTailCall {
while (left < right) {
// 哨兵划分操作
int pivot = _partition(nums, left, right);
// 对两个子数组中较短的那个执行快
// 对两个子数组中较短的那个执行快速排序
if (pivot - left < right - pivot) {
quickSort(nums, left, pivot - 1); // 递归排序左子数组
left = pivot + 1; // 剩余未排序区间为 [pivot + 1, right]

View File

@@ -12,7 +12,7 @@ int digit(int _num, int exp) {
/* 计数排序(根据 nums 第 k 位排序) */
void countingSortDigit(List<int> nums, int exp) {
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶数组
List<int> counter = List<int>.filled(10, 0);
int n = nums.length;
// 统计 0~9 各数字的出现次数

View File

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

View File

@@ -35,11 +35,11 @@ func insert(nums []int, num int, index int) {
for i := len(nums) - 1; i > index; i-- {
nums[i] = nums[i-1]
}
// 将 num 赋给 index 处元素
// 将 num 赋给 index 处元素
nums[index] = num
}
/* 删除索引 index 处元素 */
/* 删除索引 index 处元素 */
func remove(nums []int, index int) {
// 把索引 index 之后的所有元素向前移动一位
for i := index; i < len(nums)-1; i++ {

Some files were not shown because too many files have changed in this diff Show More