mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
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:
@ -37,11 +37,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 处的元素 */
|
||||
static void remove(int[] nums, int index) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (int i = index; i < nums.length - 1; i++) {
|
||||
|
||||
@ -57,7 +57,7 @@ public class linked_list {
|
||||
ListNode n2 = new ListNode(2);
|
||||
ListNode n3 = new ListNode(5);
|
||||
ListNode n4 = new ListNode(4);
|
||||
// 构建引用指向
|
||||
// 构建节点之间的引用
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
|
||||
@ -28,7 +28,7 @@ public class list {
|
||||
nums.clear();
|
||||
System.out.println("清空列表后 nums = " + nums);
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
@ -36,7 +36,7 @@ public class list {
|
||||
nums.add(4);
|
||||
System.out.println("添加元素后 nums = " + nums);
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums.add(3, 6);
|
||||
System.out.println("在索引 3 处插入数字 6 ,得到 nums = " + nums);
|
||||
|
||||
|
||||
@ -8,11 +8,11 @@ package chapter_array_and_linkedlist;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* 列表类简易实现 */
|
||||
/* 列表类 */
|
||||
class MyList {
|
||||
private int[] arr; // 数组(存储列表元素)
|
||||
private int capacity = 10; // 列表容量
|
||||
private int size = 0; // 列表长度(即当前元素数量)
|
||||
private int size = 0; // 列表长度(当前元素数量)
|
||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造方法 */
|
||||
@ -20,7 +20,7 @@ class MyList {
|
||||
arr = new int[capacity];
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量) */
|
||||
/* 获取列表长度(当前元素数量) */
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
@ -45,7 +45,7 @@ class MyList {
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
public void add(int num) {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == capacity())
|
||||
@ -55,7 +55,7 @@ class MyList {
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
public void insert(int index, int num) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("索引越界");
|
||||
@ -111,7 +111,7 @@ public class my_list {
|
||||
public static void main(String[] args) {
|
||||
/* 初始化列表 */
|
||||
MyList nums = new MyList();
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
@ -120,7 +120,7 @@ public class my_list {
|
||||
System.out.println("列表 nums = " + Arrays.toString(nums.toArray()) +
|
||||
" ,容量 = " + nums.capacity() + " ,长度 = " + nums.size());
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums.insert(3, 6);
|
||||
System.out.println("在索引 3 处插入数字 6 ,得到 nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
|
||||
@ -26,7 +26,7 @@ public class n_queens {
|
||||
// 计算该格子对应的主对角线和副对角线
|
||||
int diag1 = row - col + n - 1;
|
||||
int diag2 = row + col;
|
||||
// 剪枝:不允许该格子所在列、主对角线、副对角线存在皇后
|
||||
// 剪枝:不允许该格子所在列、主对角线、副对角线上存在皇后
|
||||
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
|
||||
// 尝试:将皇后放置在该格子
|
||||
state.get(row).set(col, "Q");
|
||||
@ -52,8 +52,8 @@ public class n_queens {
|
||||
state.add(row);
|
||||
}
|
||||
boolean[] cols = new boolean[n]; // 记录列是否有皇后
|
||||
boolean[] diags1 = new boolean[2 * n - 1]; // 记录主对角线是否有皇后
|
||||
boolean[] diags2 = new boolean[2 * n - 1]; // 记录副对角线是否有皇后
|
||||
boolean[] diags1 = new boolean[2 * n - 1]; // 记录主对角线上是否有皇后
|
||||
boolean[] diags2 = new boolean[2 * n - 1]; // 记录副对角线上是否有皇后
|
||||
List<List<List<String>>> res = new ArrayList<>();
|
||||
|
||||
backtrack(0, n, state, res, cols, diags1, diags2);
|
||||
|
||||
@ -33,7 +33,7 @@ public class iteration {
|
||||
static int whileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 初始化条件变量
|
||||
// 循环求和 1, 4, ...
|
||||
// 循环求和 1, 4, 10, ...
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// 更新条件变量
|
||||
|
||||
@ -17,7 +17,7 @@ public class hanota {
|
||||
tar.add(pan);
|
||||
}
|
||||
|
||||
/* 求解汉诺塔:问题 f(i) */
|
||||
/* 求解汉诺塔问题 f(i) */
|
||||
static void dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {
|
||||
// 若 src 只剩下一个圆盘,则直接将其移到 tar
|
||||
if (i == 1) {
|
||||
@ -32,7 +32,7 @@ public class hanota {
|
||||
dfs(i - 1, buf, src, tar);
|
||||
}
|
||||
|
||||
/* 求解汉诺塔 */
|
||||
/* 求解汉诺塔问题 */
|
||||
static void solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {
|
||||
int n = A.size();
|
||||
// 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
|
||||
@ -27,7 +27,7 @@ public class climbing_stairs_backtrack {
|
||||
|
||||
/* 爬楼梯:回溯 */
|
||||
public static int climbingStairsBacktrack(int n) {
|
||||
List<Integer> choices = Arrays.asList(1, 2); // 可选择向上爬 1 或 2 阶
|
||||
List<Integer> choices = Arrays.asList(1, 2); // 可选择向上爬 1 阶或 2 阶
|
||||
int state = 0; // 从第 0 阶开始爬
|
||||
List<Integer> res = new ArrayList<>();
|
||||
res.add(0); // 使用 res[0] 记录方案数量
|
||||
|
||||
@ -19,7 +19,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) {
|
||||
|
||||
@ -68,7 +68,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.charAt(i - 1) == t.charAt(j - 1)) {
|
||||
|
||||
@ -12,11 +12,11 @@ public class knapsack {
|
||||
|
||||
/* 0-1 背包:暴力搜索 */
|
||||
static 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);
|
||||
}
|
||||
@ -29,7 +29,7 @@ public class knapsack {
|
||||
|
||||
/* 0-1 背包:记忆化搜索 */
|
||||
static int knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {
|
||||
// 若已选完所有物品或背包无容量,则返回价值 0
|
||||
// 若已选完所有物品或背包无剩余容量,则返回价值 0
|
||||
if (i == 0 || c == 0) {
|
||||
return 0;
|
||||
}
|
||||
@ -37,7 +37,7 @@ public class knapsack {
|
||||
if (mem[i][c] != -1) {
|
||||
return mem[i][c];
|
||||
}
|
||||
// 若超过背包容量,则只能不放入背包
|
||||
// 若超过背包容量,则只能选择不放入背包
|
||||
if (wgt[i - 1] > c) {
|
||||
return knapsackDFSMem(wgt, val, mem, i - 1, c);
|
||||
}
|
||||
|
||||
@ -62,7 +62,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];
|
||||
|
||||
@ -11,7 +11,7 @@ import utils.*;
|
||||
|
||||
/* 基于邻接表实现的无向图类 */
|
||||
class GraphAdjList {
|
||||
// 邻接表,key: 顶点,value:该顶点的所有邻接顶点
|
||||
// 邻接表,key:顶点,value:该顶点的所有邻接顶点
|
||||
Map<Vertex, List<Vertex>> adjList;
|
||||
|
||||
/* 构造方法 */
|
||||
|
||||
@ -71,7 +71,7 @@ class GraphAdjMat {
|
||||
// 索引越界与相等处理
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
|
||||
throw new IndexOutOfBoundsException();
|
||||
// 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i)
|
||||
// 在无向图中,邻接矩阵关于主对角线对称,即满足 (i, j) == (j, i)
|
||||
adjMat.get(i).set(j, 1);
|
||||
adjMat.get(j).set(i, 1);
|
||||
}
|
||||
|
||||
@ -28,7 +28,7 @@ public class graph_bfs {
|
||||
// 遍历该顶点的所有邻接顶点
|
||||
for (Vertex adjVet : graph.adjList.get(vet)) {
|
||||
if (visited.contains(adjVet))
|
||||
continue; // 跳过已被访问过的顶点
|
||||
continue; // 跳过已被访问的顶点
|
||||
que.offer(adjVet); // 只入队未访问的顶点
|
||||
visited.add(adjVet); // 标记该顶点已被访问
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ public class graph_dfs {
|
||||
// 遍历该顶点的所有邻接顶点
|
||||
for (Vertex adjVet : graph.adjList.get(vet)) {
|
||||
if (visited.contains(adjVet))
|
||||
continue; // 跳过已被访问过的顶点
|
||||
continue; // 跳过已被访问的顶点
|
||||
// 递归访问邻接顶点
|
||||
dfs(graph, visited, res, adjVet);
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ class Pair {
|
||||
}
|
||||
}
|
||||
|
||||
/* 基于数组简易实现的哈希表 */
|
||||
/* 基于数组实现的哈希表 */
|
||||
class ArrayHashMap {
|
||||
private List<Pair> buckets;
|
||||
|
||||
|
||||
@ -89,7 +89,7 @@ class MaxHeap {
|
||||
// 判空处理
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||
// 交换根节点与最右叶节点(交换首元素与尾元素)
|
||||
swap(0, size() - 1);
|
||||
// 删除节点
|
||||
int val = maxHeap.remove(size() - 1);
|
||||
|
||||
@ -25,9 +25,9 @@ public class binary_search {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
/* 二分查找(左闭右开区间) */
|
||||
static 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) {
|
||||
@ -51,7 +51,7 @@ public class binary_search {
|
||||
int index = binarySearch(nums, target);
|
||||
System.out.println("目标元素 6 的索引 = " + index);
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
/* 二分查找(左闭右开区间) */
|
||||
index = binarySearchLCRO(nums, target);
|
||||
System.out.println("目标元素 6 的索引 = " + index);
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@ public class two_sum {
|
||||
/* 方法一:暴力枚举 */
|
||||
static 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)
|
||||
@ -25,9 +25,9 @@ public class two_sum {
|
||||
/* 方法二:辅助哈希表 */
|
||||
static int[] twoSumHashTable(int[] nums, int target) {
|
||||
int size = nums.length;
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
// 辅助哈希表,空间复杂度为 O(n)
|
||||
Map<Integer, Integer> dic = new HashMap<>();
|
||||
// 单层循环,时间复杂度 O(n)
|
||||
// 单层循环,时间复杂度为 O(n)
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.containsKey(target - nums[i])) {
|
||||
return new int[] { dic.get(target - nums[i]), i };
|
||||
|
||||
@ -19,7 +19,7 @@ public class bucket_sort {
|
||||
}
|
||||
// 1. 将数组元素分配到各个桶中
|
||||
for (float num : nums) {
|
||||
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
|
||||
// 输入数据范围为 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
|
||||
int i = (int) (num * k);
|
||||
// 将 num 添加进桶 i
|
||||
buckets.get(i).add(num);
|
||||
|
||||
@ -40,7 +40,7 @@ public class heap_sort {
|
||||
}
|
||||
// 从堆中提取最大元素,循环 n-1 轮
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||
// 交换根节点与最右叶节点(交换首元素与尾元素)
|
||||
int tmp = nums[0];
|
||||
nums[0] = nums[i];
|
||||
nums[i] = tmp;
|
||||
|
||||
@ -19,7 +19,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])
|
||||
@ -72,7 +72,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])
|
||||
@ -109,7 +109,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])
|
||||
@ -128,7 +128,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]
|
||||
|
||||
@ -17,7 +17,7 @@ public class radix_sort {
|
||||
|
||||
/* 计数排序(根据 nums 第 k 位排序) */
|
||||
static void countingSortDigit(int[] nums, int exp) {
|
||||
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
|
||||
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶数组
|
||||
int[] counter = new int[10];
|
||||
int n = nums.length;
|
||||
// 统计 0~9 各数字的出现次数
|
||||
|
||||
@ -42,7 +42,7 @@ class LinkedListDeque {
|
||||
/* 入队操作 */
|
||||
private void push(int num, boolean isFront) {
|
||||
ListNode node = new ListNode(num);
|
||||
// 若链表为空,则令 front, rear 都指向 node
|
||||
// 若链表为空,则令 front 和 rear 都指向 node
|
||||
if (isEmpty())
|
||||
front = rear = node;
|
||||
// 队首入队操作
|
||||
|
||||
@ -17,7 +17,7 @@ public class binary_tree {
|
||||
TreeNode n3 = new TreeNode(3);
|
||||
TreeNode n4 = new TreeNode(4);
|
||||
TreeNode n5 = new TreeNode(5);
|
||||
// 构建引用指向(即指针)
|
||||
// 构建节点之间的引用(指针)
|
||||
n1.left = n2;
|
||||
n1.right = n3;
|
||||
n2.left = n4;
|
||||
|
||||
Reference in New Issue
Block a user