mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-06 14:27:26 +08:00
Update the book based on the revised second edition (#1014)
* Revised the book * Update the book with the second revised edition * Revise base on the manuscript of the first edition
This commit is contained in:
@ -32,7 +32,7 @@ class MyList {
|
||||
|
||||
/* 访问元素 */
|
||||
public int get(int index) {
|
||||
// 索引如果越界则抛出异常,下同
|
||||
// 索引如果越界,则抛出异常,下同
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("索引越界");
|
||||
return arr[index];
|
||||
@ -82,13 +82,13 @@ class MyList {
|
||||
}
|
||||
// 更新元素数量
|
||||
size--;
|
||||
// 返回被删除元素
|
||||
// 返回被删除的元素
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
public void extendCapacity() {
|
||||
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组拷贝到新数组
|
||||
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组复制到新数组
|
||||
arr = Arrays.copyOf(arr, capacity() * extendRatio);
|
||||
// 更新列表容量
|
||||
capacity = arr.length;
|
||||
|
@ -23,10 +23,10 @@ public class n_queens {
|
||||
}
|
||||
// 遍历所有列
|
||||
for (int col = 0; col < n; col++) {
|
||||
// 计算该格子对应的主对角线和副对角线
|
||||
// 计算该格子对应的主对角线和次对角线
|
||||
int diag1 = row - col + n - 1;
|
||||
int diag2 = row + col;
|
||||
// 剪枝:不允许该格子所在列、主对角线、副对角线上存在皇后
|
||||
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
|
||||
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
|
||||
// 尝试:将皇后放置在该格子
|
||||
state.get(row).set(col, "Q");
|
||||
@ -53,7 +53,7 @@ public class n_queens {
|
||||
}
|
||||
boolean[] cols = new boolean[n]; // 记录列是否有皇后
|
||||
boolean[] diags1 = new boolean[2 * n - 1]; // 记录主对角线上是否有皇后
|
||||
boolean[] diags2 = 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);
|
||||
|
@ -10,7 +10,7 @@ import java.util.*;
|
||||
import utils.*;
|
||||
|
||||
public class graph_bfs {
|
||||
/* 广度优先遍历 BFS */
|
||||
/* 广度优先遍历 */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
|
||||
// 顶点遍历序列
|
||||
@ -47,7 +47,7 @@ public class graph_bfs {
|
||||
System.out.println("\n初始化后,图为");
|
||||
graph.print();
|
||||
|
||||
/* 广度优先遍历 BFS */
|
||||
/* 广度优先遍历 */
|
||||
List<Vertex> res = graphBFS(graph, v[0]);
|
||||
System.out.println("\n广度优先遍历(BFS)顶点序列为");
|
||||
System.out.println(Vertex.vetsToVals(res));
|
||||
|
@ -10,7 +10,7 @@ import java.util.*;
|
||||
import utils.*;
|
||||
|
||||
public class graph_dfs {
|
||||
/* 深度优先遍历 DFS 辅助函数 */
|
||||
/* 深度优先遍历辅助函数 */
|
||||
static void dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {
|
||||
res.add(vet); // 记录访问顶点
|
||||
visited.add(vet); // 标记该顶点已被访问
|
||||
@ -23,7 +23,7 @@ public class graph_dfs {
|
||||
}
|
||||
}
|
||||
|
||||
/* 深度优先遍历 DFS */
|
||||
/* 深度优先遍历 */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
static List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
|
||||
// 顶点遍历序列
|
||||
@ -43,7 +43,7 @@ public class graph_dfs {
|
||||
System.out.println("\n初始化后,图为");
|
||||
graph.print();
|
||||
|
||||
/* 深度优先遍历 DFS */
|
||||
/* 深度优先遍历 */
|
||||
List<Vertex> res = graphDFS(graph, v[0]);
|
||||
System.out.println("\n深度优先遍历(DFS)顶点序列为");
|
||||
System.out.println(Vertex.vetsToVals(res));
|
||||
|
@ -9,7 +9,7 @@ package chapter_greedy;
|
||||
public class max_capacity {
|
||||
/* 最大容量:贪心 */
|
||||
static int maxCapacity(int[] ht) {
|
||||
// 初始化 i, j 分列数组两端
|
||||
// 初始化 i, j,使其分列数组两端
|
||||
int i = 0, j = ht.length - 1;
|
||||
// 初始最大容量为 0
|
||||
int res = 0;
|
||||
|
@ -114,7 +114,7 @@ public class array_hash_map {
|
||||
map.print();
|
||||
|
||||
/* 查询操作 */
|
||||
// 向哈希表输入键 key ,得到值 value
|
||||
// 向哈希表中输入键 key ,得到值 value
|
||||
String name = map.get(15937);
|
||||
System.out.println("\n输入学号 15937 ,查询到姓名 " + name);
|
||||
|
||||
|
@ -25,7 +25,7 @@ public class hash_map {
|
||||
PrintUtil.printHashMap(map);
|
||||
|
||||
/* 查询操作 */
|
||||
// 向哈希表输入键 key ,得到值 value
|
||||
// 向哈希表中输入键 key ,得到值 value
|
||||
String name = map.get(15937);
|
||||
System.out.println("\n输入学号 15937 ,查询到姓名 " + name);
|
||||
|
||||
|
@ -43,13 +43,13 @@ class HashMapChaining {
|
||||
String get(int key) {
|
||||
int index = hashFunc(key);
|
||||
List<Pair> bucket = buckets.get(index);
|
||||
// 遍历桶,若找到 key 则返回对应 val
|
||||
// 遍历桶,若找到 key ,则返回对应 val
|
||||
for (Pair pair : bucket) {
|
||||
if (pair.key == key) {
|
||||
return pair.val;
|
||||
}
|
||||
}
|
||||
// 若未找到 key 则返回 null
|
||||
// 若未找到 key ,则返回 null
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -135,7 +135,7 @@ public class hash_map_chaining {
|
||||
map.print();
|
||||
|
||||
/* 查询操作 */
|
||||
// 向哈希表输入键 key ,得到值 value
|
||||
// 向哈希表中输入键 key ,得到值 value
|
||||
String name = map.get(13276);
|
||||
System.out.println("\n输入学号 13276 ,查询到姓名 " + name);
|
||||
|
||||
|
@ -37,9 +37,9 @@ class HashMapOpenAddressing {
|
||||
int firstTombstone = -1;
|
||||
// 线性探测,当遇到空桶时跳出
|
||||
while (buckets[index] != null) {
|
||||
// 若遇到 key ,返回对应桶索引
|
||||
// 若遇到 key ,返回对应的桶索引
|
||||
if (buckets[index].key == key) {
|
||||
// 若之前遇到了删除标记,则将键值对移动至该索引
|
||||
// 若之前遇到了删除标记,则将键值对移动至该索引处
|
||||
if (firstTombstone != -1) {
|
||||
buckets[firstTombstone] = buckets[index];
|
||||
buckets[index] = TOMBSTONE;
|
||||
@ -51,7 +51,7 @@ class HashMapOpenAddressing {
|
||||
if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {
|
||||
firstTombstone = index;
|
||||
}
|
||||
// 计算桶索引,越过尾部返回头部
|
||||
// 计算桶索引,越过尾部则返回头部
|
||||
index = (index + 1) % capacity;
|
||||
}
|
||||
// 若 key 不存在,则返回添加点的索引
|
||||
@ -145,7 +145,7 @@ public class hash_map_open_addressing {
|
||||
hashmap.print();
|
||||
|
||||
// 查询操作
|
||||
// 向哈希表输入键 key ,得到值 val
|
||||
// 向哈希表中输入键 key ,得到值 val
|
||||
String name = hashmap.get(13276);
|
||||
System.out.println("\n输入学号 13276 ,查询到姓名 " + name);
|
||||
|
||||
|
@ -24,17 +24,17 @@ class MaxHeap {
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取左子节点索引 */
|
||||
/* 获取左子节点的索引 */
|
||||
private int left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* 获取右子节点索引 */
|
||||
/* 获取右子节点的索引 */
|
||||
private int right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* 获取父节点索引 */
|
||||
/* 获取父节点的索引 */
|
||||
private int parent(int i) {
|
||||
return (i - 1) / 2; // 向下整除
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ public class bubble_sort {
|
||||
}
|
||||
}
|
||||
if (!flag)
|
||||
break; // 此轮冒泡未交换任何元素,直接跳出
|
||||
break; // 此轮“冒泡”未交换任何元素,直接跳出
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,12 +11,12 @@ import java.util.*;
|
||||
public class merge_sort {
|
||||
/* 合并左子数组和右子数组 */
|
||||
static void merge(int[] nums, int left, int mid, int right) {
|
||||
// 左子数组区间 [left, mid], 右子数组区间 [mid+1, right]
|
||||
// 左子数组区间为 [left, mid], 右子数组区间为 [mid+1, right]
|
||||
// 创建一个临时数组 tmp ,用于存放合并后的结果
|
||||
int[] tmp = new int[right - left + 1];
|
||||
// 初始化左子数组和右子数组的起始索引
|
||||
int i = left, j = mid + 1, k = 0;
|
||||
// 当左右子数组都还有元素时,比较并将较小的元素复制到临时数组中
|
||||
// 当左右子数组都还有元素时,进行比较并将较小的元素复制到临时数组中
|
||||
while (i <= mid && j <= right) {
|
||||
if (nums[i] <= nums[j])
|
||||
tmp[k++] = nums[i++];
|
||||
|
@ -54,7 +54,7 @@ class QuickSortMedian {
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* 选取三个元素的中位数 */
|
||||
/* 选取三个候选元素的中位数 */
|
||||
static int medianThree(int[] nums, int left, int mid, int right) {
|
||||
// 此处使用异或运算来简化代码
|
||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
||||
|
@ -50,7 +50,7 @@ class ArrayDeque {
|
||||
return;
|
||||
}
|
||||
// 队首指针向左移动一位
|
||||
// 通过取余操作,实现 front 越过数组头部后回到尾部
|
||||
// 通过取余操作实现 front 越过数组头部后回到尾部
|
||||
front = index(front - 1);
|
||||
// 将 num 添加至队首
|
||||
nums[front] = num;
|
||||
@ -63,7 +63,7 @@ class ArrayDeque {
|
||||
System.out.println("双向队列已满");
|
||||
return;
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
// 计算队尾指针,指向队尾索引 + 1
|
||||
int rear = index(front + queSize);
|
||||
// 将 num 添加至队尾
|
||||
nums[rear] = num;
|
||||
|
@ -40,8 +40,8 @@ class ArrayQueue {
|
||||
System.out.println("队列已满");
|
||||
return;
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
||||
// 计算队尾指针,指向队尾索引 + 1
|
||||
// 通过取余操作实现 rear 越过数组尾部后回到头部
|
||||
int rear = (front + queSize) % capacity();
|
||||
// 将 num 添加至队尾
|
||||
nums[rear] = num;
|
||||
@ -51,7 +51,7 @@ class ArrayQueue {
|
||||
/* 出队 */
|
||||
public int pop() {
|
||||
int num = peek();
|
||||
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
||||
// 队首指针向后移动一位,若越过尾部,则返回到数组头部
|
||||
front = (front + 1) % capacity();
|
||||
queSize--;
|
||||
return num;
|
||||
|
@ -30,7 +30,7 @@ class LinkedListQueue {
|
||||
|
||||
/* 入队 */
|
||||
public void push(int num) {
|
||||
// 尾节点后添加 num
|
||||
// 在尾节点后添加 num
|
||||
ListNode node = new ListNode(num);
|
||||
// 如果队列为空,则令头、尾节点都指向该节点
|
||||
if (front == null) {
|
||||
|
@ -100,7 +100,7 @@ class AVLTree {
|
||||
private TreeNode insertHelper(TreeNode node, int val) {
|
||||
if (node == null)
|
||||
return new TreeNode(val);
|
||||
/* 1. 查找插入位置,并插入节点 */
|
||||
/* 1. 查找插入位置并插入节点 */
|
||||
if (val < node.val)
|
||||
node.left = insertHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
@ -123,7 +123,7 @@ class AVLTree {
|
||||
private TreeNode removeHelper(TreeNode node, int val) {
|
||||
if (node == null)
|
||||
return null;
|
||||
/* 1. 查找节点,并删除之 */
|
||||
/* 1. 查找节点并删除 */
|
||||
if (val < node.val)
|
||||
node.left = removeHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
|
Reference in New Issue
Block a user