diff --git a/404.html b/404.html index 319de2857..f26015f38 100644 --- a/404.html +++ b/404.html @@ -1120,14 +1120,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_appendix/contribution/index.html b/chapter_appendix/contribution/index.html index f6271b3de..270f6d687 100644 --- a/chapter_appendix/contribution/index.html +++ b/chapter_appendix/contribution/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_appendix/index.html b/chapter_appendix/index.html index 3f67a04b0..47262da39 100644 --- a/chapter_appendix/index.html +++ b/chapter_appendix/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_appendix/installation/index.html b/chapter_appendix/installation/index.html index d5e77dacf..e79b18650 100644 --- a/chapter_appendix/installation/index.html +++ b/chapter_appendix/installation/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_appendix/terminology/index.html b/chapter_appendix/terminology/index.html index 441137d98..b554d6c84 100644 --- a/chapter_appendix/terminology/index.html +++ b/chapter_appendix/terminology/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_array_and_linkedlist/array/index.html b/chapter_array_and_linkedlist/array/index.html index a59c14e9e..01bbd07f2 100644 --- a/chapter_array_and_linkedlist/array/index.html +++ b/chapter_array_and_linkedlist/array/index.html @@ -1280,14 +1280,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_array_and_linkedlist/index.html b/chapter_array_and_linkedlist/index.html index 17b1d0402..3074f2359 100644 --- a/chapter_array_and_linkedlist/index.html +++ b/chapter_array_and_linkedlist/index.html @@ -1144,14 +1144,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_array_and_linkedlist/linked_list/index.html b/chapter_array_and_linkedlist/linked_list/index.html index 1478868f1..e6d0bf644 100644 --- a/chapter_array_and_linkedlist/linked_list/index.html +++ b/chapter_array_and_linkedlist/linked_list/index.html @@ -1271,14 +1271,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_array_and_linkedlist/list/index.html b/chapter_array_and_linkedlist/list/index.html index aa4cd5e7d..e307df6c5 100644 --- a/chapter_array_and_linkedlist/list/index.html +++ b/chapter_array_and_linkedlist/list/index.html @@ -1262,14 +1262,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_array_and_linkedlist/ram_and_cache/index.html b/chapter_array_and_linkedlist/ram_and_cache/index.html index 696ee6007..f24bdc638 100644 --- a/chapter_array_and_linkedlist/ram_and_cache/index.html +++ b/chapter_array_and_linkedlist/ram_and_cache/index.html @@ -1153,14 +1153,6 @@ 4.4   内存与缓存 * - - - - - - - - @@ -1172,14 +1164,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_array_and_linkedlist/summary/index.html b/chapter_array_and_linkedlist/summary/index.html index ba21d20ed..eb0414aca 100644 --- a/chapter_array_and_linkedlist/summary/index.html +++ b/chapter_array_and_linkedlist/summary/index.html @@ -1144,14 +1144,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_backtracking/backtracking_algorithm/index.html b/chapter_backtracking/backtracking_algorithm/index.html index d94f58693..e7a0b5e47 100644 --- a/chapter_backtracking/backtracking_algorithm/index.html +++ b/chapter_backtracking/backtracking_algorithm/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_backtracking/index.html b/chapter_backtracking/index.html index ea267b164..6adf194cb 100644 --- a/chapter_backtracking/index.html +++ b/chapter_backtracking/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_backtracking/n_queens_problem/index.html b/chapter_backtracking/n_queens_problem/index.html index a8422dbdb..d6e418946 100644 --- a/chapter_backtracking/n_queens_problem/index.html +++ b/chapter_backtracking/n_queens_problem/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - @@ -3704,7 +3696,11 @@

从本质上看,逐行放置策略起到了剪枝的作用,它避免了同一行出现多个皇后的所有搜索分支。

2.   列与对角线剪枝

为了满足列约束,我们可以利用一个长度为 \(n\) 的布尔型数组 cols 记录每一列是否有皇后。在每次决定放置前,我们通过 cols 将已有皇后的列进行剪枝,并在回溯中动态更新 cols 的状态。

-

那么,如何处理对角线约束呢?设棋盘中某个格子的行列索引为 \((row, col)\) ,选定矩阵中的某条主对角线,我们发现该对角线上所有格子的行索引减列索引都相等,即对角线上所有格子的 \(row - col\) 为恒定值

+
+

Tip

+

请注意,矩阵的起点位于左上角,其中行索引从上到下增加,列索引从左到右增加。

+
+

那么,如何处理对角线约束呢?设棋盘中某个格子的行列索引为 \((row, col)\) ,选定矩阵中的某条主对角线,我们发现该对角线上所有格子的行索引减列索引都相等,即主对角线上所有格子的 \(row - col\) 为恒定值

也就是说,如果两个格子满足 \(row_1 - col_1 = row_2 - col_2\) ,则它们一定处在同一条主对角线上。利用该规律,我们可以借助图 13-18 所示的数组 diags1 记录每条主对角线上是否有皇后。

同理,次对角线上的所有格子的 \(row + col\) 是恒定值。我们同样也可以借助数组 diags2 来处理次对角线约束。

处理列约束和对角线约束

diff --git a/chapter_backtracking/permutations_problem/index.html b/chapter_backtracking/permutations_problem/index.html index 217a6c1fe..cb0fdb991 100644 --- a/chapter_backtracking/permutations_problem/index.html +++ b/chapter_backtracking/permutations_problem/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_backtracking/subset_sum_problem/index.html b/chapter_backtracking/subset_sum_problem/index.html index c59f8250f..286298a0b 100644 --- a/chapter_backtracking/subset_sum_problem/index.html +++ b/chapter_backtracking/subset_sum_problem/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_backtracking/summary/index.html b/chapter_backtracking/summary/index.html index 70f940359..a3df00917 100644 --- a/chapter_backtracking/summary/index.html +++ b/chapter_backtracking/summary/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_computational_complexity/index.html b/chapter_computational_complexity/index.html index adf0b9c45..e63b9ab3d 100644 --- a/chapter_computational_complexity/index.html +++ b/chapter_computational_complexity/index.html @@ -1144,14 +1144,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_computational_complexity/iteration_and_recursion/index.html b/chapter_computational_complexity/iteration_and_recursion/index.html index a7241768e..d1b17778c 100644 --- a/chapter_computational_complexity/iteration_and_recursion/index.html +++ b/chapter_computational_complexity/iteration_and_recursion/index.html @@ -1277,14 +1277,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_computational_complexity/performance_evaluation/index.html b/chapter_computational_complexity/performance_evaluation/index.html index d859d85f9..900d018e3 100644 --- a/chapter_computational_complexity/performance_evaluation/index.html +++ b/chapter_computational_complexity/performance_evaluation/index.html @@ -1202,14 +1202,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_computational_complexity/space_complexity/index.html b/chapter_computational_complexity/space_complexity/index.html index c1985068b..39700ba18 100644 --- a/chapter_computational_complexity/space_complexity/index.html +++ b/chapter_computational_complexity/space_complexity/index.html @@ -1271,14 +1271,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_computational_complexity/summary/index.html b/chapter_computational_complexity/summary/index.html index 6afbf3548..fa567b66d 100644 --- a/chapter_computational_complexity/summary/index.html +++ b/chapter_computational_complexity/summary/index.html @@ -1202,14 +1202,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_computational_complexity/time_complexity/index.html b/chapter_computational_complexity/time_complexity/index.html index 7feb62208..23050dd21 100644 --- a/chapter_computational_complexity/time_complexity/index.html +++ b/chapter_computational_complexity/time_complexity/index.html @@ -1322,14 +1322,6 @@ 4.4   内存与缓存 * - - - - - - - - @@ -3931,7 +3923,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for (int i = 0; i < n; i++) { // 1 ns cout << 0 << endl; // 5 ns } } @@ -3944,7 +3936,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for (int i = 0; i < n; i++) { // 1 ns System.out.println(0); // 5 ns } } @@ -3957,7 +3949,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for (int i = 0; i < n; i++) { // 1 ns Console.WriteLine(0); // 5 ns } } @@ -3996,7 +3988,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for(let i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for(let i = 0; i < n; i++) { // 1 ns console.log(0); // 5 ns } } @@ -4009,7 +4001,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for(let i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for(let i = 0; i < n; i++) { // 1 ns console.log(0); // 5 ns } } @@ -4022,7 +4014,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for (int i = 0; i < n; i++) { // 1 ns print(0); // 5 ns } } @@ -4035,7 +4027,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for _ in 0..n { // 1 ns ,每轮都要执行 i++ + for _ in 0..n { // 1 ns println!("{}", 0); // 5 ns } } @@ -4048,7 +4040,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for (int i = 0; i < n; i++) { // 1 ns printf("%d", 0); // 5 ns } } @@ -4061,7 +4053,7 @@ a = a + 1 // 1 ns a = a * 2 // 10 ns // 循环 n 次 - for (i in 0..<n) { // 1 ns ,每轮都要执行 i++ + for (i in 0..<n) { // 1 ns println(0) // 5 ns } } @@ -6734,7 +6726,7 @@ O(\log_m n) = O(\log_k n / \log_k m) = O(\log_k n) return 1; } let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2); - for _ in 0..n as i32 { + for _ in 0..n { count += 1; } return count; diff --git a/chapter_data_structure/basic_data_types/index.html b/chapter_data_structure/basic_data_types/index.html index a60318a80..426822041 100644 --- a/chapter_data_structure/basic_data_types/index.html +++ b/chapter_data_structure/basic_data_types/index.html @@ -1154,14 +1154,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_data_structure/character_encoding/index.html b/chapter_data_structure/character_encoding/index.html index f1ed3a14b..7ead70ea8 100644 --- a/chapter_data_structure/character_encoding/index.html +++ b/chapter_data_structure/character_encoding/index.html @@ -1229,14 +1229,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_data_structure/classification_of_data_structure/index.html b/chapter_data_structure/classification_of_data_structure/index.html index 63f8709fa..12541a23a 100644 --- a/chapter_data_structure/classification_of_data_structure/index.html +++ b/chapter_data_structure/classification_of_data_structure/index.html @@ -1202,14 +1202,6 @@ 4.4   内存与缓存 * - - - - - - - - @@ -3690,7 +3682,7 @@

Tip

值得说明的是,将内存比作 Excel 表格是一个简化的类比,实际内存的工作机制比较复杂,涉及地址空间、内存管理、缓存机制、虚拟内存和物理内存等概念。

-

内存是所有程序的共享资源,当某块内存被某个程序占用时,则无法被其他程序同时使用了。因此在数据结构与算法的设计中,内存资源是一个重要的考虑因素。比如,算法所占用的内存峰值不应超过系统剩余空闲内存;如果缺少连续大块的内存空间,那么所选用的数据结构必须能够存储在分散的内存空间内。

+

内存是所有程序的共享资源,当某块内存被某个程序占用时,则通常无法被其他程序同时使用了。因此在数据结构与算法的设计中,内存资源是一个重要的考虑因素。比如,算法所占用的内存峰值不应超过系统剩余空闲内存;如果缺少连续大块的内存空间,那么所选用的数据结构必须能够存储在分散的内存空间内。

如图 3-3 所示,物理结构反映了数据在计算机内存中的存储方式,可分为连续空间存储(数组)和分散空间存储(链表)。物理结构从底层决定了数据的访问、更新、增删等操作方法,两种物理结构在时间效率和空间效率方面呈现出互补的特点。

连续空间存储与分散空间存储

图 3-3   连续空间存储与分散空间存储

diff --git a/chapter_data_structure/index.html b/chapter_data_structure/index.html index 7395f8934..5cc27549c 100644 --- a/chapter_data_structure/index.html +++ b/chapter_data_structure/index.html @@ -1144,14 +1144,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_data_structure/number_encoding/index.html b/chapter_data_structure/number_encoding/index.html index 7bd31648f..24cf25aaa 100644 --- a/chapter_data_structure/number_encoding/index.html +++ b/chapter_data_structure/number_encoding/index.html @@ -1202,14 +1202,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_data_structure/summary/index.html b/chapter_data_structure/summary/index.html index a69d47cc9..06ca949cb 100644 --- a/chapter_data_structure/summary/index.html +++ b/chapter_data_structure/summary/index.html @@ -1202,14 +1202,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_divide_and_conquer/binary_search_recur/index.html b/chapter_divide_and_conquer/binary_search_recur/index.html index da0892dab..7870180f9 100644 --- a/chapter_divide_and_conquer/binary_search_recur/index.html +++ b/chapter_divide_and_conquer/binary_search_recur/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - @@ -3810,7 +3802,7 @@ // 递归子问题 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 { 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 522456492..46a91f11a 100644 --- a/chapter_divide_and_conquer/build_binary_tree_problem/index.html +++ b/chapter_divide_and_conquer/build_binary_tree_problem/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_divide_and_conquer/divide_and_conquer/index.html b/chapter_divide_and_conquer/divide_and_conquer/index.html index baba98006..7b0389176 100644 --- a/chapter_divide_and_conquer/divide_and_conquer/index.html +++ b/chapter_divide_and_conquer/divide_and_conquer/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_divide_and_conquer/hanota_problem/index.html b/chapter_divide_and_conquer/hanota_problem/index.html index a5f871714..e8ea94ba9 100644 --- a/chapter_divide_and_conquer/hanota_problem/index.html +++ b/chapter_divide_and_conquer/hanota_problem/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_divide_and_conquer/index.html b/chapter_divide_and_conquer/index.html index c52bdcc7e..82ccc1c35 100644 --- a/chapter_divide_and_conquer/index.html +++ b/chapter_divide_and_conquer/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_divide_and_conquer/summary/index.html b/chapter_divide_and_conquer/summary/index.html index 75336ef42..16b0edefe 100644 --- a/chapter_divide_and_conquer/summary/index.html +++ b/chapter_divide_and_conquer/summary/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_dynamic_programming/dp_problem_features/index.html b/chapter_dynamic_programming/dp_problem_features/index.html index 6af217a71..8a183b6f9 100644 --- a/chapter_dynamic_programming/dp_problem_features/index.html +++ b/chapter_dynamic_programming/dp_problem_features/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_dynamic_programming/dp_solution_pipeline/index.html b/chapter_dynamic_programming/dp_solution_pipeline/index.html index 0f7fb9e2c..6ff284ff0 100644 --- a/chapter_dynamic_programming/dp_solution_pipeline/index.html +++ b/chapter_dynamic_programming/dp_solution_pipeline/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_dynamic_programming/edit_distance_problem/index.html b/chapter_dynamic_programming/edit_distance_problem/index.html index 492b58d92..547aa8984 100644 --- a/chapter_dynamic_programming/edit_distance_problem/index.html +++ b/chapter_dynamic_programming/edit_distance_problem/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_dynamic_programming/index.html b/chapter_dynamic_programming/index.html index 23001e311..d86a8d34b 100644 --- a/chapter_dynamic_programming/index.html +++ b/chapter_dynamic_programming/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_dynamic_programming/intro_to_dynamic_programming/index.html b/chapter_dynamic_programming/intro_to_dynamic_programming/index.html index 5b4aca461..42351910e 100644 --- a/chapter_dynamic_programming/intro_to_dynamic_programming/index.html +++ b/chapter_dynamic_programming/intro_to_dynamic_programming/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_dynamic_programming/knapsack_problem/index.html b/chapter_dynamic_programming/knapsack_problem/index.html index f02dbcdbb..f19eeb00c 100644 --- a/chapter_dynamic_programming/knapsack_problem/index.html +++ b/chapter_dynamic_programming/knapsack_problem/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_dynamic_programming/summary/index.html b/chapter_dynamic_programming/summary/index.html index 6c17f5ce1..3a83387d4 100644 --- a/chapter_dynamic_programming/summary/index.html +++ b/chapter_dynamic_programming/summary/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_dynamic_programming/unbounded_knapsack_problem/index.html b/chapter_dynamic_programming/unbounded_knapsack_problem/index.html index ec28b33be..8898f2f28 100644 --- a/chapter_dynamic_programming/unbounded_knapsack_problem/index.html +++ b/chapter_dynamic_programming/unbounded_knapsack_problem/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_graph/graph/index.html b/chapter_graph/graph/index.html index 31022aadf..a99fc9754 100644 --- a/chapter_graph/graph/index.html +++ b/chapter_graph/graph/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_graph/graph_operations/index.html b/chapter_graph/graph_operations/index.html index b33e84614..2f9a64c82 100644 --- a/chapter_graph/graph_operations/index.html +++ b/chapter_graph/graph_operations/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_graph/graph_traversal/index.html b/chapter_graph/graph_traversal/index.html index 2df6d1713..a4dd49032 100644 --- a/chapter_graph/graph_traversal/index.html +++ b/chapter_graph/graph_traversal/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_graph/index.html b/chapter_graph/index.html index 08ac52abb..c24b32158 100644 --- a/chapter_graph/index.html +++ b/chapter_graph/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_graph/summary/index.html b/chapter_graph/summary/index.html index 6a275f2a2..6dddcd4f3 100644 --- a/chapter_graph/summary/index.html +++ b/chapter_graph/summary/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_greedy/fractional_knapsack_problem/index.html b/chapter_greedy/fractional_knapsack_problem/index.html index ac96cc0cc..2188a274c 100644 --- a/chapter_greedy/fractional_knapsack_problem/index.html +++ b/chapter_greedy/fractional_knapsack_problem/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_greedy/greedy_algorithm/index.html b/chapter_greedy/greedy_algorithm/index.html index 8fc34e8c5..a9e751d98 100644 --- a/chapter_greedy/greedy_algorithm/index.html +++ b/chapter_greedy/greedy_algorithm/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_greedy/index.html b/chapter_greedy/index.html index a1c83860b..a8d06a565 100644 --- a/chapter_greedy/index.html +++ b/chapter_greedy/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_greedy/max_capacity_problem/index.html b/chapter_greedy/max_capacity_problem/index.html index bd8b8c51f..b2b2a49c9 100644 --- a/chapter_greedy/max_capacity_problem/index.html +++ b/chapter_greedy/max_capacity_problem/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_greedy/max_product_cutting_problem/index.html b/chapter_greedy/max_product_cutting_problem/index.html index 59b2986de..3f5289d75 100644 --- a/chapter_greedy/max_product_cutting_problem/index.html +++ b/chapter_greedy/max_product_cutting_problem/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_greedy/summary/index.html b/chapter_greedy/summary/index.html index afe1d414e..e79e2f22d 100644 --- a/chapter_greedy/summary/index.html +++ b/chapter_greedy/summary/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_hashing/hash_algorithm/index.html b/chapter_hashing/hash_algorithm/index.html index aeed30da0..0618f6656 100644 --- a/chapter_hashing/hash_algorithm/index.html +++ b/chapter_hashing/hash_algorithm/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_hashing/hash_collision/index.html b/chapter_hashing/hash_collision/index.html index b90ed98f5..c5373e910 100644 --- a/chapter_hashing/hash_collision/index.html +++ b/chapter_hashing/hash_collision/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_hashing/hash_map/index.html b/chapter_hashing/hash_map/index.html index 700b31f25..1b4a83005 100644 --- a/chapter_hashing/hash_map/index.html +++ b/chapter_hashing/hash_map/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_hashing/index.html b/chapter_hashing/index.html index 47d611657..06d3ef67e 100644 --- a/chapter_hashing/index.html +++ b/chapter_hashing/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_hashing/summary/index.html b/chapter_hashing/summary/index.html index 37b37c24f..d4959100c 100644 --- a/chapter_hashing/summary/index.html +++ b/chapter_hashing/summary/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_heap/build_heap/index.html b/chapter_heap/build_heap/index.html index b55ed484b..ecbf78a53 100644 --- a/chapter_heap/build_heap/index.html +++ b/chapter_heap/build_heap/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_heap/heap/index.html b/chapter_heap/heap/index.html index cbdb21b5f..715ab73c8 100644 --- a/chapter_heap/heap/index.html +++ b/chapter_heap/heap/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_heap/index.html b/chapter_heap/index.html index f72ed60e7..c0fdf3f02 100644 --- a/chapter_heap/index.html +++ b/chapter_heap/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_heap/summary/index.html b/chapter_heap/summary/index.html index 928002226..0edea03f8 100644 --- a/chapter_heap/summary/index.html +++ b/chapter_heap/summary/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_heap/top_k/index.html b/chapter_heap/top_k/index.html index 085b11701..35b7d9f71 100644 --- a/chapter_heap/top_k/index.html +++ b/chapter_heap/top_k/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_hello_algo/index.html b/chapter_hello_algo/index.html index 8c8ae9e66..f0dbe9eac 100644 --- a/chapter_hello_algo/index.html +++ b/chapter_hello_algo/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_introduction/algorithms_are_everywhere/index.html b/chapter_introduction/algorithms_are_everywhere/index.html index 9093f1f9d..6183cce04 100644 --- a/chapter_introduction/algorithms_are_everywhere/index.html +++ b/chapter_introduction/algorithms_are_everywhere/index.html @@ -1154,14 +1154,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_introduction/index.html b/chapter_introduction/index.html index 3d18f9f7b..b4e08ec6e 100644 --- a/chapter_introduction/index.html +++ b/chapter_introduction/index.html @@ -1144,14 +1144,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_introduction/summary/index.html b/chapter_introduction/summary/index.html index a5e0a61bb..fd15f79c9 100644 --- a/chapter_introduction/summary/index.html +++ b/chapter_introduction/summary/index.html @@ -1193,14 +1193,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_introduction/what_is_dsa/index.html b/chapter_introduction/what_is_dsa/index.html index 649b71ed0..02c96ddb3 100644 --- a/chapter_introduction/what_is_dsa/index.html +++ b/chapter_introduction/what_is_dsa/index.html @@ -1211,14 +1211,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_paperbook/index.html b/chapter_paperbook/index.html index e708041bc..0530304d7 100644 --- a/chapter_paperbook/index.html +++ b/chapter_paperbook/index.html @@ -1140,14 +1140,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_preface/about_the_book/index.html b/chapter_preface/about_the_book/index.html index 3a45e87d2..26598c19a 100644 --- a/chapter_preface/about_the_book/index.html +++ b/chapter_preface/about_the_book/index.html @@ -1211,14 +1211,6 @@ 4.4   内存与缓存 * - - - - - - - - @@ -3684,9 +3676,9 @@

0.1   关于本书

本项目旨在创建一本开源、免费、对新手友好的数据结构与算法入门教程。

0.1.1   读者对象

若你是算法初学者,从未接触过算法,或者已经有一些刷题经验,对数据结构与算法有模糊的认识,在会与不会之间反复横跳,那么本书正是为你量身定制的!

@@ -3708,7 +3700,7 @@

0.1.3   致谢

本书在开源社区众多贡献者的共同努力下不断完善。感谢每一位投入时间与精力的撰稿人,他们是(按照 GitHub 自动生成的顺序):krahets、Gonglja、nuomi1、codingonion、Reanon、justin-tse、hpstory、danielsss、curtishd、night-cruise、S-N-O-R-L-A-X、msk397、gvenusleo、RiverTwilight、gyt95、zhuoqinyue、Zuoxun、mingXta、hello-ikun、khoaxuantu、FangYuan33、GN-Yu、longsizhuo、mgisr、Cathay-Chen、guowei-gong、xBLACKICEx、K3v123、IsChristina、JoseHung、qualifier1024、pengchzn、Guanngxu、QiLOL、L-Super、WSL0809、Slone123c、lhxsm、yuan0221、what-is-me、rongyi、JeffersonHuang、longranger2、theNefelibatas、yuelinxin、xiongsp、nanlei、a16su、cy-by-side、gaofer、malone6、Wonderdch、hongyun-robot、XiaChuerwu、yd-j、bluebean-cloud、iron-irax、he-weilai、Nigh、MolDuM、Phoenix0415、XC-Zero、SamJin98、reeswell、NI-SW、Horbin-Magician、xjr7670、YangXuanyi、DullSword、iStig、qq909244296、jiaxianhua、wenjianmin、keshida、kilikilikid、lclc6、lwbaptx、luluxia、boloboloda、hts0000、gledfish、fbigm、echo1937、szu17dmy、dshlstarr、coderlef、czruby、beintentional、KeiichiKasai、xb534、ElaBosak233、baagod、zhouLion、yishangzhang、yi427、yabo083、weibk、wangwang105、th1nk3r-ing、tao363、4yDX3906、syd168、siqyka、selear、sdshaoda、noobcodemaker、chadyi、lyl625760、lucaswangdev、liuxjerry、0130w、shanghai-Jerry、JackYang-hellobobo、Javesun99、lipusheng、ShiMaRing、FreddieLi、FloranceYeh、Transmigration-zhou、fanchenggang、gltianwen、Dr-XYZ、curly210102、CuB3y0nd、youshaoXG、bubble9um、fanenr、52coder、foursevenlove、KorsChen、ZongYangL、hezhizhen、linzeyan、ZJKung、GaochaoZhu、yang-le、Evilrabbit520、Turing-1024-Lee、Suremotoo、Allen-Scai、Richard-Zhang1019、qingpeng9802、primexiao、nidhoggfgg、1ch0、MwumLi、ZnYang2018、hugtyftg、logan-qiu、psychelzh 和 Keynman 。

-

本书的代码审阅工作由 codingonion、curtishd、Gonglja、gvenusleo、hpstory、justin-tse、krahets、night-cruise、nuomi1 和 Reanon 完成(按照首字母顺序排列)。感谢他们付出的时间与精力,正是他们确保了各语言代码的规范与统一。

+

本书的代码审阅工作由 codingonion、curtishd、Gonglja、gvenusleo、hpstory、justin-tse、khoaxuantu、krahets、night-cruise、nuomi1 和 Reanon 完成(按照首字母顺序排列)。感谢他们付出的时间与精力,正是他们确保了各语言代码的规范与统一。

在本书的创作过程中,我得到了许多人的帮助。

排序算法对比

diff --git a/chapter_stack_and_queue/deque/index.html b/chapter_stack_and_queue/deque/index.html index fa7f9bf74..2bc41234e 100644 --- a/chapter_stack_and_queue/deque/index.html +++ b/chapter_stack_and_queue/deque/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_stack_and_queue/index.html b/chapter_stack_and_queue/index.html index a58ac6f08..25fd6fae0 100644 --- a/chapter_stack_and_queue/index.html +++ b/chapter_stack_and_queue/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_stack_and_queue/queue/index.html b/chapter_stack_and_queue/queue/index.html index 39fa6eb22..185ff0699 100644 --- a/chapter_stack_and_queue/queue/index.html +++ b/chapter_stack_and_queue/queue/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_stack_and_queue/stack/index.html b/chapter_stack_and_queue/stack/index.html index 3a5030336..bd4fbf8f3 100644 --- a/chapter_stack_and_queue/stack/index.html +++ b/chapter_stack_and_queue/stack/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_stack_and_queue/summary/index.html b/chapter_stack_and_queue/summary/index.html index fe479e26c..a426d177d 100644 --- a/chapter_stack_and_queue/summary/index.html +++ b/chapter_stack_and_queue/summary/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_tree/array_representation_of_tree/index.html b/chapter_tree/array_representation_of_tree/index.html index 7d3609ed0..766cf3733 100644 --- a/chapter_tree/array_representation_of_tree/index.html +++ b/chapter_tree/array_representation_of_tree/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_tree/avl_tree/index.html b/chapter_tree/avl_tree/index.html index 4f658f17c..df3456ea8 100644 --- a/chapter_tree/avl_tree/index.html +++ b/chapter_tree/avl_tree/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_tree/binary_search_tree/index.html b/chapter_tree/binary_search_tree/index.html index 0c05d0930..8cb408032 100644 --- a/chapter_tree/binary_search_tree/index.html +++ b/chapter_tree/binary_search_tree/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_tree/binary_tree/index.html b/chapter_tree/binary_tree/index.html index 146cf9a49..3c6f1f1c2 100644 --- a/chapter_tree/binary_tree/index.html +++ b/chapter_tree/binary_tree/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_tree/binary_tree_traversal/index.html b/chapter_tree/binary_tree_traversal/index.html index dfc7a36f9..ea6538fa6 100644 --- a/chapter_tree/binary_tree_traversal/index.html +++ b/chapter_tree/binary_tree_traversal/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_tree/index.html b/chapter_tree/index.html index 81d3d8614..7bec2d6f0 100644 --- a/chapter_tree/index.html +++ b/chapter_tree/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/chapter_tree/summary/index.html b/chapter_tree/summary/index.html index 9ac13d15e..c82b384df 100644 --- a/chapter_tree/summary/index.html +++ b/chapter_tree/summary/index.html @@ -1142,14 +1142,6 @@ 4.4   内存与缓存 * - - - - - - - - diff --git a/en/chapter_backtracking/n_queens_problem/index.html b/en/chapter_backtracking/n_queens_problem/index.html index b3a1a5029..6723d32f7 100644 --- a/en/chapter_backtracking/n_queens_problem/index.html +++ b/en/chapter_backtracking/n_queens_problem/index.html @@ -3631,6 +3631,10 @@

Essentially, the row-by-row placing strategy serves as a pruning function, avoiding all search branches that would place multiple queens in the same row.

2.   Column and diagonal pruning

To satisfy column constraints, we can use a boolean array cols of length \(n\) to track whether a queen occupies each column. Before each placement decision, cols is used to prune the columns that already have queens, and it is dynamically updated during backtracking.

+
+

Tip

+

Note that the origin of the chessboard is located in the upper left corner, where the row index increases from top to bottom, and the column index increases from left to right.

+

How about the diagonal constraints? Let the row and column indices of a cell on the chessboard be \((row, col)\). By selecting a specific main diagonal, we notice that the difference \(row - col\) is the same for all cells on that diagonal, meaning that \(row - col\) is a constant value on that diagonal.

Thus, if two cells satisfy \(row_1 - col_1 = row_2 - col_2\), they are definitely on the same main diagonal. Using this pattern, we can utilize the array diags1 shown in Figure 13-18 to track whether a queen is on any main diagonal.

Similarly, the sum \(row + col\) is a constant value for all cells on a secondary diagonal. We can also use the array diags2 to handle secondary diagonal constraints.

diff --git a/en/chapter_graph/graph/index.html b/en/chapter_graph/graph/index.html index b781a507a..5dcb3bfe0 100644 --- a/en/chapter_graph/graph/index.html +++ b/en/chapter_graph/graph/index.html @@ -1957,9 +1957,9 @@