diff --git a/404.html b/404.html index 5cb284ee9..51ee917ed 100644 --- a/404.html +++ b/404.html @@ -1756,6 +1756,8 @@ + + @@ -1824,14 +1826,50 @@ +
由于作者能力有限,书中难免存在一些遗漏和错误,请您谅解。如果您发现了笔误、失效链接、内容缺失、文字歧义、解释不清晰或行文结构不合理等问题,请协助我们进行修正,以帮助其他读者获得更优质的学习资源。
+所有撰稿人的 GitHub ID 将在仓库、网页版和 PDF 版的主页上进行展示,以感谢他们对开源社区的无私奉献。
开源的魅力
-纸质书籍的两次印刷的间隔时间往往需要数年,内容更新非常不方便。但在本开源书中,内容更迭的时间被缩短至数日甚至几个小时。
+纸质书籍的两次印刷的间隔时间往往需要数年,内容更新非常不方便。
+然而在本开源书中,内容更迭的时间被缩短至数日甚至几个小时。
由于作者能力有限,书中难免存在一些遗漏和错误,请您谅解。如果您发现了笔误、失效链接、内容缺失、文字歧义、解释不清晰或行文结构不合理等问题,请协助我们进行修正,以帮助其他读者获得更优质的学习资源。所有撰稿人将在仓库和网站主页上展示,以感谢他们对开源社区的无私奉献!
在每个页面的右上角有一个「编辑」图标,您可以按照以下步骤修改文本或代码:
Fig. 页面编辑按键
-由于图片无法直接修改,因此需要通过新建 Issue 或评论留言来描述问题,我们会尽快重新绘制并替换图片。
+图片无法直接修改,需要通过新建 Issue 或评论留言来描述问题,我们会尽快重新绘制并替换图片。
如果您有兴趣参与此开源项目,包括将代码翻译成其他编程语言、扩展文章内容等,那么需要实施 Pull Request 工作流程:
git clone
命令将仓库克隆至本地。/* 在数组中查找指定元素 */
function find(nums, target) {
for (let i = 0; i < nums.length; i++) {
- if (nums[i] == target) return i;
+ if (nums[i] === target) return i;
}
return -1;
}
diff --git a/chapter_array_and_linkedlist/index.html b/chapter_array_and_linkedlist/index.html
index 856dee6d6..d4e71d7d9 100644
--- a/chapter_array_and_linkedlist/index.html
+++ b/chapter_array_and_linkedlist/index.html
@@ -1769,6 +1769,8 @@
+
+
@@ -1837,14 +1839,50 @@
+
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1862,7 +1900,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1882,7 +1920,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1902,7 +1940,7 @@
- 10.5. 小结
+ 10.6. 小结
@@ -3318,9 +3356,8 @@
Abstract
-数组的砖块整齐排列,紧贴在一起。
-链表的砖块分散各处,连接的藤蔓自由地穿梭于砖缝之间。
-它们共同构成了数据结构的世界。
+数据结构的世界如同一睹厚实的砖墙。
+数组的砖块整齐排列,逐个紧贴。链表的砖块分散各处,连接的藤蔓自由地穿梭于砖缝之间。
Abstract
-我们如同迷宫中的探索者,在寻找出口的道路上可能会遇到困难。
-回溯的力量让我们能够重新开始,最终寻找到正确的道路。
+我们如同迷宫中的探索者,在前进的道路上可能会遇到困难。
+回溯的力量让我们能够重新开始,不断尝试,最终找到通往光明的出口。
[class]{}-[func]{backtrack}
-
-[class]{}-[func]{subsetSumINaive}
+subset_sum_i_naive.js/* 回溯算法:子集和 I */
+function backtrack(state, target, total, choices, res) {
+ // 子集和等于 target 时,记录解
+ if (total === target) {
+ res.push([...state]);
+ return;
+ }
+ // 遍历所有选择
+ for (let i = 0; i < choices.length; i++) {
+ // 剪枝:若子集和超过 target ,则跳过该选择
+ if (total + choices[i] > target) {
+ continue;
+ }
+ // 尝试:做出选择,更新元素和 total
+ state.push(choices[i]);
+ // 进行下一轮选择
+ backtrack(state, target, total + choices[i], choices, res);
+ // 回退:撤销选择,恢复到之前的状态
+ state.pop();
+ }
+}
+
+/* 求解子集和 I(包含重复子集) */
+function subsetSumINaive(nums, target) {
+ const state = []; // 状态(子集)
+ const total = 0; // 子集和
+ const res = []; // 结果列表(子集列表)
+ backtrack(state, target, total, nums, res);
+ return res;
+}
[class]{}-[func]{backtrack}
-
-[class]{}-[func]{subsetSumINaive}
+subset_sum_i_naive.ts/* 回溯算法:子集和 I */
+function backtrack(
+ state: number[],
+ target: number,
+ total: number,
+ choices: number[],
+ res: number[][]
+): void {
+ // 子集和等于 target 时,记录解
+ if (total === target) {
+ res.push([...state]);
+ return;
+ }
+ // 遍历所有选择
+ for (let i = 0; i < choices.length; i++) {
+ // 剪枝:若子集和超过 target ,则跳过该选择
+ if (total + choices[i] > target) {
+ continue;
+ }
+ // 尝试:做出选择,更新元素和 total
+ state.push(choices[i]);
+ // 进行下一轮选择
+ backtrack(state, target, total + choices[i], choices, res);
+ // 回退:撤销选择,恢复到之前的状态
+ state.pop();
+ }
+}
+
+/* 求解子集和 I(包含重复子集) */
+function subsetSumINaive(nums: number[], target: number): number[][] {
+ const state = []; // 状态(子集)
+ const total = 0; // 子集和
+ const res = []; // 结果列表(子集列表)
+ backtrack(state, target, total, nums, res);
+ return res;
+}
[class]{}-[func]{backtrack}
-
-[class]{}-[func]{subsetSumI}
+subset_sum_i.js/* 回溯算法:子集和 I */
+function backtrack(state, target, choices, start, res) {
+ // 子集和等于 target 时,记录解
+ if (target === 0) {
+ res.push([...state]);
+ return;
+ }
+ // 遍历所有选择
+ // 剪枝二:从 start 开始遍历,避免生成重复子集
+ for (let i = start; i < choices.length; i++) {
+ // 剪枝一:若子集和超过 target ,则直接结束循环
+ // 这是因为数组已排序,后边元素更大,子集和一定超过 target
+ if (target - choices[i] < 0) {
+ break;
+ }
+ // 尝试:做出选择,更新 target, start
+ state.push(choices[i]);
+ // 进行下一轮选择
+ backtrack(state, target - choices[i], choices, i, res);
+ // 回退:撤销选择,恢复到之前的状态
+ state.pop();
+ }
+}
+
+/* 求解子集和 I */
+function subsetSumI(nums, target) {
+ const state = []; // 状态(子集)
+ nums.sort(); // 对 nums 进行排序
+ const start = 0; // 遍历起始点
+ const res = []; // 结果列表(子集列表)
+ backtrack(state, target, nums, start, res);
+ return res;
+}
[class]{}-[func]{backtrack}
-
-[class]{}-[func]{subsetSumI}
+subset_sum_i.ts/* 回溯算法:子集和 I */
+function backtrack(
+ state: number[],
+ target: number,
+ choices: number[],
+ start: number,
+ res: number[][]
+): void {
+ // 子集和等于 target 时,记录解
+ if (target === 0) {
+ res.push([...state]);
+ return;
+ }
+ // 遍历所有选择
+ // 剪枝二:从 start 开始遍历,避免生成重复子集
+ for (let i = start; i < choices.length; i++) {
+ // 剪枝一:若子集和超过 target ,则直接结束循环
+ // 这是因为数组已排序,后边元素更大,子集和一定超过 target
+ if (target - choices[i] < 0) {
+ break;
+ }
+ // 尝试:做出选择,更新 target, start
+ state.push(choices[i]);
+ // 进行下一轮选择
+ backtrack(state, target - choices[i], choices, i, res);
+ // 回退:撤销选择,恢复到之前的状态
+ state.pop();
+ }
+}
+
+/* 求解子集和 I */
+function subsetSumI(nums: number[], target: number): number[][] {
+ const state = []; // 状态(子集)
+ nums.sort(); // 对 nums 进行排序
+ const start = 0; // 遍历起始点
+ const res = []; // 结果列表(子集列表)
+ backtrack(state, target, nums, start, res);
+ return res;
+}
[class]{}-[func]{backtrack}
-
-[class]{}-[func]{subsetSumII}
+subset_sum_ii.js/* 回溯算法:子集和 II */
+function backtrack(state, target, choices, start, res) {
+ // 子集和等于 target 时,记录解
+ if (target === 0) {
+ res.push([...state]);
+ return;
+ }
+ // 遍历所有选择
+ // 剪枝二:从 start 开始遍历,避免生成重复子集
+ // 剪枝三:从 start 开始遍历,避免重复选择同一元素
+ for (let i = start; i < choices.length; i++) {
+ // 剪枝一:若子集和超过 target ,则直接结束循环
+ // 这是因为数组已排序,后边元素更大,子集和一定超过 target
+ if (target - choices[i] < 0) {
+ break;
+ }
+ // 剪枝四:如果该元素与左边元素相等,说明该搜索分支重复,直接跳过
+ if (i > start && choices[i] === choices[i - 1]) {
+ continue;
+ }
+ // 尝试:做出选择,更新 target, start
+ state.push(choices[i]);
+ // 进行下一轮选择
+ backtrack(state, target - choices[i], choices, i + 1, res);
+ // 回退:撤销选择,恢复到之前的状态
+ state.pop();
+ }
+}
+
+/* 求解子集和 II */
+function subsetSumII(nums, target) {
+ const state = []; // 状态(子集)
+ nums.sort(); // 对 nums 进行排序
+ const start = 0; // 遍历起始点
+ const res = []; // 结果列表(子集列表)
+ backtrack(state, target, nums, start, res);
+ return res;
+}
[class]{}-[func]{backtrack}
-
-[class]{}-[func]{subsetSumII}
+subset_sum_ii.ts/* 回溯算法:子集和 II */
+function backtrack(
+ state: number[],
+ target: number,
+ choices: number[],
+ start: number,
+ res: number[][]
+): void {
+ // 子集和等于 target 时,记录解
+ if (target === 0) {
+ res.push([...state]);
+ return;
+ }
+ // 遍历所有选择
+ // 剪枝二:从 start 开始遍历,避免生成重复子集
+ // 剪枝三:从 start 开始遍历,避免重复选择同一元素
+ for (let i = start; i < choices.length; i++) {
+ // 剪枝一:若子集和超过 target ,则直接结束循环
+ // 这是因为数组已排序,后边元素更大,子集和一定超过 target
+ if (target - choices[i] < 0) {
+ break;
+ }
+ // 剪枝四:如果该元素与左边元素相等,说明该搜索分支重复,直接跳过
+ if (i > start && choices[i] === choices[i - 1]) {
+ continue;
+ }
+ // 尝试:做出选择,更新 target, start
+ state.push(choices[i]);
+ // 进行下一轮选择
+ backtrack(state, target - choices[i], choices, i + 1, res);
+ // 回退:撤销选择,恢复到之前的状态
+ state.pop();
+ }
+}
+
+/* 求解子集和 II */
+function subsetSumII(nums: number[], target: number): number[][] {
+ const state = []; // 状态(子集)
+ nums.sort(); // 对 nums 进行排序
+ const start = 0; // 遍历起始点
+ const res = []; // 结果列表(子集列表)
+ backtrack(state, target, nums, start, res);
+ return res;
+}
/* 指数阶(递归实现) */
function expRecur(n) {
- if (n == 1) return 1;
+ if (n === 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
/* 指数阶(递归实现) */
function expRecur(n: number): number {
- if (n == 1) return 1;
+ if (n === 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
/* 阶乘阶(递归实现) */
function factorialRecur(n) {
- if (n == 0) return 1;
+ if (n === 0) return 1;
let count = 0;
// 从 1 个分裂出 n 个
for (let i = 0; i < n; i++) {
@@ -6039,7 +6077,7 @@ n! = n \times (n - 1) \times (n - 2) \times \cdots \times 2 \times 1
time_complexity.ts/* 阶乘阶(递归实现) */
function factorialRecur(n: number): number {
- if (n == 0) return 1;
+ if (n === 0) return 1;
let count = 0;
// 从 1 个分裂出 n 个
for (let i = 0; i < n; i++) {
diff --git a/chapter_data_structure/basic_data_types/index.html b/chapter_data_structure/basic_data_types/index.html
index e04f70574..656806f9e 100644
--- a/chapter_data_structure/basic_data_types/index.html
+++ b/chapter_data_structure/basic_data_types/index.html
@@ -1779,6 +1779,8 @@
+
+
@@ -1847,14 +1849,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1872,7 +1910,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1892,7 +1930,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1912,7 +1950,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_data_structure/character_encoding/index.html b/chapter_data_structure/character_encoding/index.html
index 9a980c9d5..649cf7ded 100644
--- a/chapter_data_structure/character_encoding/index.html
+++ b/chapter_data_structure/character_encoding/index.html
@@ -1844,6 +1844,8 @@
+
+
@@ -1912,14 +1914,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1937,7 +1975,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1957,7 +1995,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1977,7 +2015,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_data_structure/classification_of_data_structure/index.html b/chapter_data_structure/classification_of_data_structure/index.html
index 277418582..1519eabe9 100644
--- a/chapter_data_structure/classification_of_data_structure/index.html
+++ b/chapter_data_structure/classification_of_data_structure/index.html
@@ -1823,6 +1823,8 @@
+
+
@@ -1891,14 +1893,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1916,7 +1954,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1936,7 +1974,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1956,7 +1994,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_data_structure/index.html b/chapter_data_structure/index.html
index 009194783..3f8a2fc32 100644
--- a/chapter_data_structure/index.html
+++ b/chapter_data_structure/index.html
@@ -1769,6 +1769,8 @@
+
+
@@ -1837,14 +1839,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1862,7 +1900,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1882,7 +1920,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1902,7 +1940,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_data_structure/number_encoding/index.html b/chapter_data_structure/number_encoding/index.html
index bbfa735f6..69f6ba7bb 100644
--- a/chapter_data_structure/number_encoding/index.html
+++ b/chapter_data_structure/number_encoding/index.html
@@ -1823,6 +1823,8 @@
+
+
@@ -1891,14 +1893,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1916,7 +1954,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1936,7 +1974,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1956,7 +1994,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_data_structure/summary/index.html b/chapter_data_structure/summary/index.html
index 7defcc0ea..c85ae82a9 100644
--- a/chapter_data_structure/summary/index.html
+++ b/chapter_data_structure/summary/index.html
@@ -1816,6 +1816,8 @@
+
+
@@ -1884,14 +1886,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1909,7 +1947,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1929,7 +1967,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1949,7 +1987,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_divide_and_conquer/binary_search_recur/index.html b/chapter_divide_and_conquer/binary_search_recur/index.html
index 741f671c0..5848b96b0 100644
--- a/chapter_divide_and_conquer/binary_search_recur/index.html
+++ b/chapter_divide_and_conquer/binary_search_recur/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
@@ -3520,15 +3558,61 @@
-binary_search_recur.js[class]{}-[func]{dfs}
-
-[class]{}-[func]{binarySearch}
+binary_search_recur.js/* 二分查找:问题 f(i, j) */
+function dfs(nums, target, i, j) {
+ // 若区间为空,代表无目标元素,则返回 -1
+ if (i > j) {
+ return -1;
+ }
+ // 计算中点索引 m
+ const m = i + ((j - i) >> 1);
+ if (nums[m] < target) {
+ // 递归子问题 f(m+1, j)
+ return dfs(nums, target, m + 1, j);
+ } else if (nums[m] > target) {
+ // 递归子问题 f(i, m-1)
+ return dfs(nums, target, i, m - 1);
+ } else {
+ // 找到目标元素,返回其索引
+ return m;
+ }
+}
+
+/* 二分查找 */
+function binarySearch(nums, target) {
+ const n = nums.length;
+ // 求解问题 f(0, n-1)
+ return dfs(nums, target, 0, n - 1);
+}
-binary_search_recur.ts[class]{}-[func]{dfs}
-
-[class]{}-[func]{binarySearch}
+binary_search_recur.ts/* 二分查找:问题 f(i, j) */
+function dfs(nums: number[], target: number, i: number, j: number): number {
+ // 若区间为空,代表无目标元素,则返回 -1
+ if (i > j) {
+ return -1;
+ }
+ // 计算中点索引 m
+ const m = i + ((j - i) >> 1);
+ if (nums[m] < target) {
+ // 递归子问题 f(m+1, j)
+ return dfs(nums, target, m + 1, j);
+ } else if (nums[m] > target) {
+ // 递归子问题 f(i, m-1)
+ return dfs(nums, target, i, m - 1);
+ } else {
+ // 找到目标元素,返回其索引
+ return m;
+ }
+}
+
+/* 二分查找 */
+function binarySearch(nums: number[], target: number): number {
+ const n = nums.length;
+ // 求解问题 f(0, n-1)
+ return dfs(nums, target, 0, n - 1);
+}
diff --git a/chapter_divide_and_conquer/build_binary_tree_problem/index.html b/chapter_divide_and_conquer/build_binary_tree_problem/index.html
index 68699232f..16087e008 100644
--- a/chapter_divide_and_conquer/build_binary_tree_problem/index.html
+++ b/chapter_divide_and_conquer/build_binary_tree_problem/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
@@ -3609,15 +3647,68 @@
-build_tree.js[class]{}-[func]{dfs}
-
-[class]{}-[func]{buildTree}
+build_tree.js/* 构建二叉树:分治 */
+function dfs(preorder, inorder, hmap, i, l, r) {
+ // 子树区间为空时终止
+ if (r - l < 0) return null;
+ // 初始化根节点
+ const root = new TreeNode(preorder[i]);
+ // 查询 m ,从而划分左右子树
+ const m = hmap.get(preorder[i]);
+ // 子问题:构建左子树
+ root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1);
+ // 子问题:构建右子树
+ root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r);
+ // 返回根节点
+ return root;
+}
+
+/* 构建二叉树 */
+function buildTree(preorder, inorder) {
+ // 初始化哈希表,存储 inorder 元素到索引的映射
+ let hmap = new Map();
+ for (let i = 0; i < inorder.length; i++) {
+ hmap.set(inorder[i], i);
+ }
+ const root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1);
+ return root;
+}
-build_tree.ts[class]{}-[func]{dfs}
-
-[class]{}-[func]{buildTree}
+build_tree.ts/* 构建二叉树:分治 */
+function dfs(
+ preorder: number[],
+ inorder: number[],
+ hmap: Map<number, number>,
+ i: number,
+ l: number,
+ r: number
+): TreeNode | null {
+ // 子树区间为空时终止
+ if (r - l < 0) return null;
+ // 初始化根节点
+ const root: TreeNode = new TreeNode(preorder[i]);
+ // 查询 m ,从而划分左右子树
+ const m = hmap.get(preorder[i]);
+ // 子问题:构建左子树
+ root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1);
+ // 子问题:构建右子树
+ root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r);
+ // 返回根节点
+ return root;
+}
+
+/* 构建二叉树 */
+function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
+ // 初始化哈希表,存储 inorder 元素到索引的映射
+ let hmap = new Map<number, number>();
+ for (let i = 0; i < inorder.length; i++) {
+ hmap.set(inorder[i], i);
+ }
+ const root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1);
+ return root;
+}
diff --git a/chapter_divide_and_conquer/divide_and_conquer/index.html b/chapter_divide_and_conquer/divide_and_conquer/index.html
index ecc6d6e0e..b8745578c 100644
--- a/chapter_divide_and_conquer/divide_and_conquer/index.html
+++ b/chapter_divide_and_conquer/divide_and_conquer/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_divide_and_conquer/hanota_problem/index.html b/chapter_divide_and_conquer/hanota_problem/index.html
index 73445a464..40b5e47ec 100644
--- a/chapter_divide_and_conquer/hanota_problem/index.html
+++ b/chapter_divide_and_conquer/hanota_problem/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
@@ -3612,19 +3650,67 @@
-hanota.js[class]{}-[func]{move}
-
-[class]{}-[func]{dfs}
-
-[class]{}-[func]{hanota}
+hanota.js/* 移动一个圆盘 */
+function move(src, tar) {
+ // 从 src 顶部拿出一个圆盘
+ const pan = src.pop();
+ // 将圆盘放入 tar 顶部
+ tar.push(pan);
+}
+
+/* 求解汉诺塔:问题 f(i) */
+function dfs(i, src, buf, tar) {
+ // 若 src 只剩下一个圆盘,则直接将其移到 tar
+ if (i === 1) {
+ move(src, tar);
+ return;
+ }
+ // 子问题 f(i-1) :将 src 顶部 i-1 个圆盘借助 tar 移到 buf
+ dfs(i - 1, src, tar, buf);
+ // 子问题 f(1) :将 src 剩余一个圆盘移到 tar
+ move(src, tar);
+ // 子问题 f(i-1) :将 buf 顶部 i-1 个圆盘借助 src 移到 tar
+ dfs(i - 1, buf, src, tar);
+}
+
+/* 求解汉诺塔 */
+function hanota(A, B, C) {
+ const n = A.length;
+ // 将 A 顶部 n 个圆盘借助 B 移到 C
+ dfs(n, A, B, C);
+}
-hanota.ts[class]{}-[func]{move}
-
-[class]{}-[func]{dfs}
-
-[class]{}-[func]{hanota}
+hanota.ts/* 移动一个圆盘 */
+function move(src: number[], tar: number[]): void {
+ // 从 src 顶部拿出一个圆盘
+ const pan = src.pop();
+ // 将圆盘放入 tar 顶部
+ tar.push(pan);
+}
+
+/* 求解汉诺塔:问题 f(i) */
+function dfs(i: number, src: number[], buf: number[], tar: number[]): void {
+ // 若 src 只剩下一个圆盘,则直接将其移到 tar
+ if (i === 1) {
+ move(src, tar);
+ return;
+ }
+ // 子问题 f(i-1) :将 src 顶部 i-1 个圆盘借助 tar 移到 buf
+ dfs(i - 1, src, tar, buf);
+ // 子问题 f(1) :将 src 剩余一个圆盘移到 tar
+ move(src, tar);
+ // 子问题 f(i-1) :将 buf 顶部 i-1 个圆盘借助 src 移到 tar
+ dfs(i - 1, buf, src, tar);
+}
+
+/* 求解汉诺塔 */
+function hanota(A: number[], B: number[], C: number[]): void {
+ const n = A.length;
+ // 将 A 顶部 n 个圆盘借助 B 移到 C
+ dfs(n, A, B, C);
+}
diff --git a/chapter_divide_and_conquer/index.html b/chapter_divide_and_conquer/index.html
index b0c2230c8..759417704 100644
--- a/chapter_divide_and_conquer/index.html
+++ b/chapter_divide_and_conquer/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
@@ -3318,8 +3356,8 @@
Abstract
-分治一次又一次地拆解难题,每一次的拆解都让问题变得更为简单。
-从简单做起,一切都不再复杂。
+难题被逐层拆解,每一次的拆解都使它变得更为简单。
+分而治之揭示了一个重要的事实:从简单做起,一切都不再复杂。
本章内容¶
diff --git a/chapter_divide_and_conquer/summary/index.html b/chapter_divide_and_conquer/summary/index.html
index dca000c9c..aba7e9833 100644
--- a/chapter_divide_and_conquer/summary/index.html
+++ b/chapter_divide_and_conquer/summary/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_dynamic_programming/dp_problem_features/index.html b/chapter_dynamic_programming/dp_problem_features/index.html
index 7d6e26c5f..dad4cd450 100644
--- a/chapter_dynamic_programming/dp_problem_features/index.html
+++ b/chapter_dynamic_programming/dp_problem_features/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_dynamic_programming/dp_solution_pipeline/index.html b/chapter_dynamic_programming/dp_solution_pipeline/index.html
index 5a3c7f04f..e5ac00e48 100644
--- a/chapter_dynamic_programming/dp_solution_pipeline/index.html
+++ b/chapter_dynamic_programming/dp_solution_pipeline/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_dynamic_programming/edit_distance_problem/index.html b/chapter_dynamic_programming/edit_distance_problem/index.html
index d8d928ba9..4bfd55142 100644
--- a/chapter_dynamic_programming/edit_distance_problem/index.html
+++ b/chapter_dynamic_programming/edit_distance_problem/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_dynamic_programming/index.html b/chapter_dynamic_programming/index.html
index bcb415187..77e104f36 100644
--- a/chapter_dynamic_programming/index.html
+++ b/chapter_dynamic_programming/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_dynamic_programming/intro_to_dynamic_programming/index.html b/chapter_dynamic_programming/intro_to_dynamic_programming/index.html
index 9c3a981dd..4ce2e2d33 100644
--- a/chapter_dynamic_programming/intro_to_dynamic_programming/index.html
+++ b/chapter_dynamic_programming/intro_to_dynamic_programming/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
@@ -3812,7 +3850,7 @@ dp[i] = dp[i-1] + dp[i-2]
climbing_stairs_dfs.js/* 搜索 */
function dfs(i) {
// 已知 dp[1] 和 dp[2] ,返回之
- if (i == 1 || i == 2) return i;
+ if (i === 1 || i === 2) return i;
// dp[i] = dp[i-1] + dp[i-2]
const count = dfs(i - 1) + dfs(i - 2);
return count;
@@ -3828,7 +3866,7 @@ dp[i] = dp[i-1] + dp[i-2]
climbing_stairs_dfs.ts/* 搜索 */
function dfs(i: number): number {
// 已知 dp[1] 和 dp[2] ,返回之
- if (i == 1 || i == 2) return i;
+ if (i === 1 || i === 2) return i;
// dp[i] = dp[i-1] + dp[i-2]
const count = dfs(i - 1) + dfs(i - 2);
return count;
@@ -4041,7 +4079,7 @@ dp[i] = dp[i-1] + dp[i-2]
climbing_stairs_dfs_mem.js/* 记忆化搜索 */
function dfs(i, mem) {
// 已知 dp[1] 和 dp[2] ,返回之
- if (i == 1 || i == 2) return i;
+ if (i === 1 || i === 2) return i;
// 若存在记录 dp[i] ,则直接返回之
if (mem[i] != -1) return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
@@ -4063,7 +4101,7 @@ dp[i] = dp[i-1] + dp[i-2]
climbing_stairs_dfs_mem.ts/* 记忆化搜索 */
function dfs(i: number, mem: number[]): number {
// 已知 dp[1] 和 dp[2] ,返回之
- if (i == 1 || i == 2) return i;
+ if (i === 1 || i === 2) return i;
// 若存在记录 dp[i] ,则直接返回之
if (mem[i] != -1) return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
@@ -4277,7 +4315,7 @@ dp[i] = dp[i-1] + dp[i-2]
climbing_stairs_dp.js/* 爬楼梯:动态规划 */
function climbingStairsDP(n) {
- if (n == 1 || n == 2) return n;
+ if (n === 1 || n === 2) return n;
// 初始化 dp 表,用于存储子问题的解
const dp = new Array(n + 1).fill(-1);
// 初始状态:预设最小子问题的解
@@ -4294,7 +4332,7 @@ dp[i] = dp[i-1] + dp[i-2]
climbing_stairs_dp.ts/* 爬楼梯:动态规划 */
function climbingStairsDP(n: number): number {
- if (n == 1 || n == 2) return n;
+ if (n === 1 || n === 2) return n;
// 初始化 dp 表,用于存储子问题的解
const dp = new Array(n + 1).fill(-1);
// 初始状态:预设最小子问题的解
@@ -4466,7 +4504,7 @@ dp[i] = dp[i-1] + dp[i-2]
climbing_stairs_dp.js/* 爬楼梯:状态压缩后的动态规划 */
function climbingStairsDPComp(n) {
- if (n == 1 || n == 2) return n;
+ if (n === 1 || n === 2) return n;
let a = 1,
b = 2;
for (let i = 3; i <= n; i++) {
@@ -4481,7 +4519,7 @@ dp[i] = dp[i-1] + dp[i-2]
climbing_stairs_dp.ts/* 爬楼梯:状态压缩后的动态规划 */
function climbingStairsDPComp(n: number): number {
- if (n == 1 || n == 2) return n;
+ if (n === 1 || n === 2) return n;
let a = 1,
b = 2;
for (let i = 3; i <= n; i++) {
diff --git a/chapter_dynamic_programming/knapsack_problem/index.html b/chapter_dynamic_programming/knapsack_problem/index.html
index 9c42ebd69..6ad16c28b 100644
--- a/chapter_dynamic_programming/knapsack_problem/index.html
+++ b/chapter_dynamic_programming/knapsack_problem/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_dynamic_programming/summary/index.html b/chapter_dynamic_programming/summary/index.html
index 87ef82f11..b1ab67b87 100644
--- a/chapter_dynamic_programming/summary/index.html
+++ b/chapter_dynamic_programming/summary/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_dynamic_programming/unbounded_knapsack_problem/index.html b/chapter_dynamic_programming/unbounded_knapsack_problem/index.html
index 700f68ff4..ef6e7768b 100644
--- a/chapter_dynamic_programming/unbounded_knapsack_problem/index.html
+++ b/chapter_dynamic_programming/unbounded_knapsack_problem/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_graph/graph/index.html b/chapter_graph/graph/index.html
index 476352112..30faa18d1 100644
--- a/chapter_graph/graph/index.html
+++ b/chapter_graph/graph/index.html
@@ -1857,6 +1857,8 @@
+
+
@@ -1925,14 +1927,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1950,7 +1988,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1970,7 +2008,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1990,7 +2028,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_graph/graph_operations/index.html b/chapter_graph/graph_operations/index.html
index 767737357..fd8f1db85 100644
--- a/chapter_graph/graph_operations/index.html
+++ b/chapter_graph/graph_operations/index.html
@@ -1830,6 +1830,8 @@
+
+
@@ -1898,14 +1900,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1923,7 +1961,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1943,7 +1981,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1963,7 +2001,7 @@
- 10.5. 小结
+ 10.6. 小结
@@ -3831,7 +3869,7 @@
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;
}
@@ -3921,7 +3959,7 @@
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;
}
diff --git a/chapter_graph/graph_traversal/index.html b/chapter_graph/graph_traversal/index.html
index 55ef1d3fb..04ce8c6df 100644
--- a/chapter_graph/graph_traversal/index.html
+++ b/chapter_graph/graph_traversal/index.html
@@ -1863,6 +1863,8 @@
+
+
@@ -1931,14 +1933,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1956,7 +1994,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1976,7 +2014,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1996,7 +2034,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_graph/index.html b/chapter_graph/index.html
index 35206e6d5..199cd29ec 100644
--- a/chapter_graph/index.html
+++ b/chapter_graph/index.html
@@ -1769,6 +1769,8 @@
+
+
@@ -1837,14 +1839,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1862,7 +1900,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1882,7 +1920,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1902,7 +1940,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_graph/summary/index.html b/chapter_graph/summary/index.html
index 5a69b33aa..a74fa4a82 100644
--- a/chapter_graph/summary/index.html
+++ b/chapter_graph/summary/index.html
@@ -1816,6 +1816,8 @@
+
+
@@ -1884,14 +1886,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1909,7 +1947,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1929,7 +1967,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1949,7 +1987,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_greedy/fractional_knapsack_problem/index.html b/chapter_greedy/fractional_knapsack_problem/index.html
index cea1f0a0e..22bc4b752 100644
--- a/chapter_greedy/fractional_knapsack_problem/index.html
+++ b/chapter_greedy/fractional_knapsack_problem/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_greedy/greedy_algorithm/index.html b/chapter_greedy/greedy_algorithm/index.html
index c536d0b41..7e62830ce 100644
--- a/chapter_greedy/greedy_algorithm/index.html
+++ b/chapter_greedy/greedy_algorithm/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_greedy/index.html b/chapter_greedy/index.html
index c8d853f92..b9be11d7e 100644
--- a/chapter_greedy/index.html
+++ b/chapter_greedy/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
@@ -3319,7 +3357,7 @@
Abstract
向日葵朝着太阳转动,时刻都在追求自身成长的最大可能。
-贪心策略既直接又高效,在一轮轮简单选择中逐步导向最佳答案。
+贪心策略在一轮轮的简单选择中,逐步导向最佳的答案。
本章内容¶
diff --git a/chapter_greedy/max_capacity_problem/index.html b/chapter_greedy/max_capacity_problem/index.html
index aeffc70d0..adaaa07cc 100644
--- a/chapter_greedy/max_capacity_problem/index.html
+++ b/chapter_greedy/max_capacity_problem/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_greedy/max_product_cutting_problem/index.html b/chapter_greedy/max_product_cutting_problem/index.html
index 093300b27..f5544c097 100644
--- a/chapter_greedy/max_product_cutting_problem/index.html
+++ b/chapter_greedy/max_product_cutting_problem/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_greedy/summary/index.html b/chapter_greedy/summary/index.html
index 46ac4a4d3..8386b82ad 100644
--- a/chapter_greedy/summary/index.html
+++ b/chapter_greedy/summary/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_hashing/hash_algorithm/index.html b/chapter_hashing/hash_algorithm/index.html
index 22e6b5e9d..41b8c582c 100644
--- a/chapter_hashing/hash_algorithm/index.html
+++ b/chapter_hashing/hash_algorithm/index.html
@@ -1837,6 +1837,8 @@
+
+
@@ -1905,14 +1907,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1930,7 +1968,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1950,7 +1988,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1970,7 +2008,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_hashing/hash_collision/index.html b/chapter_hashing/hash_collision/index.html
index 1b5a3a872..e6749e4cc 100644
--- a/chapter_hashing/hash_collision/index.html
+++ b/chapter_hashing/hash_collision/index.html
@@ -1850,6 +1850,8 @@
+
+
@@ -1918,14 +1920,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1943,7 +1981,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1963,7 +2001,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1983,7 +2021,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_hashing/hash_map/index.html b/chapter_hashing/hash_map/index.html
index e66ce7d81..146db3783 100644
--- a/chapter_hashing/hash_map/index.html
+++ b/chapter_hashing/hash_map/index.html
@@ -1830,6 +1830,8 @@
+
+
@@ -1898,14 +1900,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1923,7 +1961,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1943,7 +1981,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1963,7 +2001,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_hashing/index.html b/chapter_hashing/index.html
index 5dba18c68..b359f694d 100644
--- a/chapter_hashing/index.html
+++ b/chapter_hashing/index.html
@@ -1769,6 +1769,8 @@
+
+
@@ -1837,14 +1839,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1862,7 +1900,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1882,7 +1920,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1902,7 +1940,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_hashing/summary/index.html b/chapter_hashing/summary/index.html
index 65cc4dd5f..f22dbe695 100644
--- a/chapter_hashing/summary/index.html
+++ b/chapter_hashing/summary/index.html
@@ -1816,6 +1816,8 @@
+
+
@@ -1884,14 +1886,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1909,7 +1947,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1929,7 +1967,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1949,7 +1987,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_heap/build_heap/index.html b/chapter_heap/build_heap/index.html
index d35f2bfcb..90fd4e8c5 100644
--- a/chapter_heap/build_heap/index.html
+++ b/chapter_heap/build_heap/index.html
@@ -1830,6 +1830,8 @@
+
+
@@ -1898,14 +1900,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1923,7 +1961,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1943,7 +1981,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1963,7 +2001,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_heap/heap/index.html b/chapter_heap/heap/index.html
index da3a8d919..94a051bfb 100644
--- a/chapter_heap/heap/index.html
+++ b/chapter_heap/heap/index.html
@@ -1864,6 +1864,8 @@
+
+
@@ -1932,14 +1934,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1957,7 +1995,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1977,7 +2015,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1997,7 +2035,7 @@
- 10.5. 小结
+ 10.6. 小结
@@ -4631,7 +4669,7 @@
if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;
if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
- if (ma == i) break;
+ if (ma === i) break;
// 交换两节点
this.#swap(i, ma);
// 循环向下堆化
@@ -4665,7 +4703,7 @@
if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;
if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
- if (ma == i) break;
+ if (ma === i) break;
// 交换两节点
this.swap(i, ma);
// 循环向下堆化
diff --git a/chapter_heap/index.html b/chapter_heap/index.html
index 5c60a696f..870c7d4fc 100644
--- a/chapter_heap/index.html
+++ b/chapter_heap/index.html
@@ -1769,6 +1769,8 @@
+
+
@@ -1837,14 +1839,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1862,7 +1900,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1882,7 +1920,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1902,7 +1940,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_heap/summary/index.html b/chapter_heap/summary/index.html
index 23c4f5167..146de98b0 100644
--- a/chapter_heap/summary/index.html
+++ b/chapter_heap/summary/index.html
@@ -1816,6 +1816,8 @@
+
+
@@ -1884,14 +1886,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1909,7 +1947,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1929,7 +1967,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1949,7 +1987,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_heap/top_k/index.html b/chapter_heap/top_k/index.html
index 39ab62293..7e2810fb0 100644
--- a/chapter_heap/top_k/index.html
+++ b/chapter_heap/top_k/index.html
@@ -1830,6 +1830,8 @@
+
+
@@ -1898,14 +1900,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1923,7 +1961,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1943,7 +1981,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1963,7 +2001,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_introduction/algorithms_are_everywhere/index.html b/chapter_introduction/algorithms_are_everywhere/index.html
index ce248f8a5..4b69bde3a 100644
--- a/chapter_introduction/algorithms_are_everywhere/index.html
+++ b/chapter_introduction/algorithms_are_everywhere/index.html
@@ -1779,6 +1779,8 @@
+
+
@@ -1847,14 +1849,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1872,7 +1910,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1892,7 +1930,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1912,7 +1950,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_introduction/index.html b/chapter_introduction/index.html
index 9c627ee17..b908c53d6 100644
--- a/chapter_introduction/index.html
+++ b/chapter_introduction/index.html
@@ -1769,6 +1769,8 @@
+
+
@@ -1837,14 +1839,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1862,7 +1900,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1882,7 +1920,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1902,7 +1940,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_introduction/summary/index.html b/chapter_introduction/summary/index.html
index 427e7d928..e212b26f1 100644
--- a/chapter_introduction/summary/index.html
+++ b/chapter_introduction/summary/index.html
@@ -1779,6 +1779,8 @@
+
+
@@ -1847,14 +1849,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1872,7 +1910,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1892,7 +1930,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1912,7 +1950,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_introduction/what_is_dsa/index.html b/chapter_introduction/what_is_dsa/index.html
index a69346d5d..0ebb28c0f 100644
--- a/chapter_introduction/what_is_dsa/index.html
+++ b/chapter_introduction/what_is_dsa/index.html
@@ -1830,6 +1830,8 @@
+
+
@@ -1898,14 +1900,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1923,7 +1961,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1943,7 +1981,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1963,7 +2001,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_preface/about_the_book/index.html b/chapter_preface/about_the_book/index.html
index 2308f9623..0ae985f39 100644
--- a/chapter_preface/about_the_book/index.html
+++ b/chapter_preface/about_the_book/index.html
@@ -1830,6 +1830,8 @@
+
+
@@ -1898,14 +1900,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1923,7 +1961,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1943,7 +1981,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1963,7 +2001,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_preface/index.html b/chapter_preface/index.html
index 0291dee9c..ebd2844b9 100644
--- a/chapter_preface/index.html
+++ b/chapter_preface/index.html
@@ -1767,6 +1767,8 @@
+
+
@@ -1835,14 +1837,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1860,7 +1898,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1880,7 +1918,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1900,7 +1938,7 @@
- 10.5. 小结
+ 10.6. 小结
@@ -3316,8 +3354,8 @@
Abstract
-这本书是为所有想要了解并掌握算法的读者编写的。
-无论你的背景如何,都可以在这里找到属于你的学习之旅。
+算法犹如美妙的交响乐,每一行代码都像韵律般流淌。
+愿这本书在你的脑海中轻轻响起,留下独特而深刻的旋律。
本章内容¶
diff --git a/chapter_preface/suggestions/index.html b/chapter_preface/suggestions/index.html
index dbfd3918a..8d4117aee 100644
--- a/chapter_preface/suggestions/index.html
+++ b/chapter_preface/suggestions/index.html
@@ -1844,6 +1844,8 @@
+
+
@@ -1912,14 +1914,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1937,7 +1975,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1957,7 +1995,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1977,7 +2015,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_preface/summary/index.html b/chapter_preface/summary/index.html
index 91eacdba2..a1944e570 100644
--- a/chapter_preface/summary/index.html
+++ b/chapter_preface/summary/index.html
@@ -1779,6 +1779,8 @@
+
+
@@ -1847,14 +1849,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1872,7 +1910,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1892,7 +1930,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1912,7 +1950,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_reference/index.html b/chapter_reference/index.html
index 29ab616ea..d1f0375f1 100644
--- a/chapter_reference/index.html
+++ b/chapter_reference/index.html
@@ -1765,6 +1765,8 @@
+
+
@@ -1833,14 +1835,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1858,7 +1896,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1878,7 +1916,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1898,7 +1936,7 @@
- 10.5. 小结
+ 10.6. 小结
diff --git a/chapter_searching/binary_search.assets/binary_search_example.png b/chapter_searching/binary_search.assets/binary_search_example.png
new file mode 100644
index 000000000..de3492b26
Binary files /dev/null and b/chapter_searching/binary_search.assets/binary_search_example.png differ
diff --git a/chapter_searching/binary_search.assets/binary_search_ranges.png b/chapter_searching/binary_search.assets/binary_search_ranges.png
index 5456be499..387587aa1 100644
Binary files a/chapter_searching/binary_search.assets/binary_search_ranges.png and b/chapter_searching/binary_search.assets/binary_search_ranges.png differ
diff --git a/chapter_searching/binary_search.assets/binary_search_step0.png b/chapter_searching/binary_search.assets/binary_search_step0.png
deleted file mode 100644
index 1be714bbd..000000000
Binary files a/chapter_searching/binary_search.assets/binary_search_step0.png and /dev/null differ
diff --git a/chapter_searching/binary_search.assets/binary_search_step1.png b/chapter_searching/binary_search.assets/binary_search_step1.png
index 82ec2de45..f7c273671 100644
Binary files a/chapter_searching/binary_search.assets/binary_search_step1.png and b/chapter_searching/binary_search.assets/binary_search_step1.png differ
diff --git a/chapter_searching/binary_search.assets/binary_search_step2.png b/chapter_searching/binary_search.assets/binary_search_step2.png
index 5e971c425..7da591ddd 100644
Binary files a/chapter_searching/binary_search.assets/binary_search_step2.png and b/chapter_searching/binary_search.assets/binary_search_step2.png differ
diff --git a/chapter_searching/binary_search.assets/binary_search_step3.png b/chapter_searching/binary_search.assets/binary_search_step3.png
index 429a98926..f653e912e 100644
Binary files a/chapter_searching/binary_search.assets/binary_search_step3.png and b/chapter_searching/binary_search.assets/binary_search_step3.png differ
diff --git a/chapter_searching/binary_search.assets/binary_search_step4.png b/chapter_searching/binary_search.assets/binary_search_step4.png
index 86820ed32..3c63d109a 100644
Binary files a/chapter_searching/binary_search.assets/binary_search_step4.png and b/chapter_searching/binary_search.assets/binary_search_step4.png differ
diff --git a/chapter_searching/binary_search.assets/binary_search_step5.png b/chapter_searching/binary_search.assets/binary_search_step5.png
index 5e4855ef9..0477a640c 100644
Binary files a/chapter_searching/binary_search.assets/binary_search_step5.png and b/chapter_searching/binary_search.assets/binary_search_step5.png differ
diff --git a/chapter_searching/binary_search.assets/binary_search_step6.png b/chapter_searching/binary_search.assets/binary_search_step6.png
index 6de8cd607..7f348ec23 100644
Binary files a/chapter_searching/binary_search.assets/binary_search_step6.png and b/chapter_searching/binary_search.assets/binary_search_step6.png differ
diff --git a/chapter_searching/binary_search.assets/binary_search_step7.png b/chapter_searching/binary_search.assets/binary_search_step7.png
index 1b8f0fe49..196f5b1e9 100644
Binary files a/chapter_searching/binary_search.assets/binary_search_step7.png and b/chapter_searching/binary_search.assets/binary_search_step7.png differ
diff --git a/chapter_searching/binary_search/index.html b/chapter_searching/binary_search/index.html
index 2e25f3063..73f11e96c 100644
--- a/chapter_searching/binary_search/index.html
+++ b/chapter_searching/binary_search/index.html
@@ -18,7 +18,7 @@
-
+
@@ -1769,6 +1769,8 @@
+
+
@@ -1891,14 +1893,50 @@
+ -
+
+
+
+
+ 10.2. 二分查找插入点
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- 10.2. 二分查找边界
+ 10.3. 二分查找边界
+
+
+
+
+
+
+
+
@@ -1916,7 +1954,7 @@
- 10.3. 哈希优化策略
+ 10.4. 哈希优化策略
@@ -1936,7 +1974,7 @@
- 10.4. 重识搜索算法
+ 10.5. 重识搜索算法
@@ -1956,7 +1994,7 @@
- 10.5. 小结
+ 10.6. 小结
@@ -3379,7 +3417,10 @@
Question
给定一个长度为 \(n\) 的数组 nums
,元素按从小到大的顺序排列,数组不包含重复元素。请查找并返回元素 target
在该数组中的索引。若数组不包含该元素,则返回 \(-1\) 。
-对于上述问题,我们先初始化指针 \(i = 0\) 和 \(j = n - 1\) ,分别指向数组首元素和尾元素,代表搜索区间 \([0, n - 1]\) 。其中,中括号表示“闭区间”,即包含边界值本身。
+
+ Fig. 二分查找示例数据
+
+对于上述问题,我们先初始化指针 \(i = 0\) 和 \(j = n - 1\) ,分别指向数组首元素和尾元素,代表搜索区间 \([0, n - 1]\) 。请注意,中括号表示闭区间,其包含边界值本身。
接下来,循环执行以下两个步骤:
- 计算中点索引 \(m = \lfloor {(i + j) / 2} \rfloor\) ,其中 \(\lfloor \space \rfloor\) 表示向下取整操作。
@@ -3391,12 +3432,9 @@
若数组不包含目标元素,搜索区间最终会缩小为空。此时返回 \(-1\) 。
-
+