mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-21 19:06:11 +00:00
build
This commit is contained in:
@@ -162,7 +162,18 @@ comments: true
|
||||
=== "Dart"
|
||||
|
||||
```dart title="preorder_traversal_i_compact.dart"
|
||||
[class]{}-[func]{preOrder}
|
||||
/* 前序遍历:例题一 */
|
||||
void preOrder(TreeNode? root, List<TreeNode> res) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
if (root.val == 7) {
|
||||
// 记录解
|
||||
res.add(root);
|
||||
}
|
||||
preOrder(root.left, res);
|
||||
preOrder(root.right, res);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
@@ -407,7 +418,27 @@ comments: true
|
||||
=== "Dart"
|
||||
|
||||
```dart title="preorder_traversal_ii_compact.dart"
|
||||
[class]{}-[func]{preOrder}
|
||||
/* 前序遍历:例题二 */
|
||||
void preOrder(
|
||||
TreeNode? root,
|
||||
List<TreeNode> path,
|
||||
List<List<TreeNode>> res,
|
||||
) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 尝试
|
||||
path.add(root);
|
||||
if (root.val == 7) {
|
||||
// 记录解
|
||||
res.add(List.from(path));
|
||||
}
|
||||
preOrder(root.left, path, res);
|
||||
preOrder(root.right, path, res);
|
||||
// 回退
|
||||
path.removeLast();
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
@@ -711,7 +742,29 @@ comments: true
|
||||
=== "Dart"
|
||||
|
||||
```dart title="preorder_traversal_iii_compact.dart"
|
||||
[class]{}-[func]{preOrder}
|
||||
/* 前序遍历:例题三 */
|
||||
void preOrder(
|
||||
TreeNode? root,
|
||||
List<TreeNode> path,
|
||||
List<List<TreeNode>> res,
|
||||
) {
|
||||
if (root == null || root.val == 3) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 尝试
|
||||
path.add(root);
|
||||
if (root.val == 7) {
|
||||
// 记录解
|
||||
res.add(List.from(path));
|
||||
path.removeLast();
|
||||
return;
|
||||
}
|
||||
preOrder(root.left, path, res);
|
||||
preOrder(root.right, path, res);
|
||||
// 回退
|
||||
path.removeLast();
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
@@ -1499,17 +1552,55 @@ comments: true
|
||||
=== "Dart"
|
||||
|
||||
```dart title="preorder_traversal_iii_template.dart"
|
||||
[class]{}-[func]{isSolution}
|
||||
/* 判断当前状态是否为解 */
|
||||
bool isSolution(List<TreeNode> state) {
|
||||
return state.isNotEmpty && state.last.val == 7;
|
||||
}
|
||||
|
||||
[class]{}-[func]{recordSolution}
|
||||
/* 记录解 */
|
||||
void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
|
||||
res.add(List.from(state));
|
||||
}
|
||||
|
||||
[class]{}-[func]{isValid}
|
||||
/* 判断在当前状态下,该选择是否合法 */
|
||||
bool isValid(List<TreeNode> state, TreeNode? choice) {
|
||||
return choice != null && choice.val != 3;
|
||||
}
|
||||
|
||||
[class]{}-[func]{makeChoice}
|
||||
/* 更新状态 */
|
||||
void makeChoice(List<TreeNode> state, TreeNode? choice) {
|
||||
state.add(choice!);
|
||||
}
|
||||
|
||||
[class]{}-[func]{undoChoice}
|
||||
/* 恢复状态 */
|
||||
void undoChoice(List<TreeNode> state, TreeNode? choice) {
|
||||
state.removeLast();
|
||||
}
|
||||
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:例题三 */
|
||||
void backtrack(
|
||||
List<TreeNode> state,
|
||||
List<TreeNode?> choices,
|
||||
List<List<TreeNode>> res,
|
||||
) {
|
||||
// 检查是否为解
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res);
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (TreeNode? choice in choices) {
|
||||
// 剪枝:检查选择是否合法
|
||||
if (isValid(state, choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, [choice!.left, choice.right], res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
@@ -501,9 +501,61 @@ comments: true
|
||||
=== "Dart"
|
||||
|
||||
```dart title="n_queens.dart"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:N 皇后 */
|
||||
void backtrack(
|
||||
int row,
|
||||
int n,
|
||||
List<List<String>> state,
|
||||
List<List<List<String>>> res,
|
||||
List<bool> cols,
|
||||
List<bool> diags1,
|
||||
List<bool> diags2,
|
||||
) {
|
||||
// 当放置完所有行时,记录解
|
||||
if (row == n) {
|
||||
List<List<String>> copyState = [];
|
||||
for (List<String> sRow in state) {
|
||||
copyState.add(List.from(sRow));
|
||||
}
|
||||
res.add(copyState);
|
||||
return;
|
||||
}
|
||||
// 遍历所有列
|
||||
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[row][col] = "Q";
|
||||
cols[col] = true;
|
||||
diags1[diag1] = true;
|
||||
diags2[diag2] = true;
|
||||
// 放置下一行
|
||||
backtrack(row + 1, n, state, res, cols, diags1, diags2);
|
||||
// 回退:将该格子恢复为空位
|
||||
state[row][col] = "#";
|
||||
cols[col] = false;
|
||||
diags1[diag1] = false;
|
||||
diags2[diag2] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{nQueens}
|
||||
/* 求解 N 皇后 */
|
||||
List<List<List<String>>> nQueens(int n) {
|
||||
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
|
||||
List<List<String>> state = List.generate(n, (index) => List.filled(n, "#"));
|
||||
List<bool> cols = List.filled(n, false); // 记录列是否有皇后
|
||||
List<bool> diags1 = List.filled(2 * n - 1, false); // 记录主对角线是否有皇后
|
||||
List<bool> diags2 = List.filled(2 * n - 1, false); // 记录副对角线是否有皇后
|
||||
List<List<List<String>>> res = [];
|
||||
|
||||
backtrack(0, n, state, res, cols, diags1, diags2);
|
||||
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
@@ -399,9 +399,41 @@ comments: true
|
||||
=== "Dart"
|
||||
|
||||
```dart title="permutations_i.dart"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 I */
|
||||
void backtrack(
|
||||
List<int> state,
|
||||
List<int> choices,
|
||||
List<bool> selected,
|
||||
List<List<int>> res,
|
||||
) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.length == choices.length) {
|
||||
res.add(List.from(state));
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (int i = 0; i < choices.length; i++) {
|
||||
int choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i]) {
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true;
|
||||
state.add(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
/* 全排列 I */
|
||||
List<List<int>> permutationsI(List<int> nums) {
|
||||
List<List<int>> res = [];
|
||||
backtrack([], nums, List.filled(nums.length, false), res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
@@ -791,9 +823,43 @@ comments: true
|
||||
=== "Dart"
|
||||
|
||||
```dart title="permutations_ii.dart"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 II */
|
||||
void backtrack(
|
||||
List<int> state,
|
||||
List<int> choices,
|
||||
List<bool> selected,
|
||||
List<List<int>> res,
|
||||
) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.length == choices.length) {
|
||||
res.add(List.from(state));
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
Set<int> duplicated = {};
|
||||
for (int i = 0; i < choices.length; i++) {
|
||||
int choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i] && !duplicated.contains(choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
duplicated.add(choice); // 记录选择过的元素值
|
||||
selected[i] = true;
|
||||
state.add(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsII}
|
||||
/* 全排列 II */
|
||||
List<List<int>> permutationsII(List<int> nums) {
|
||||
List<List<int>> res = [];
|
||||
backtrack([], nums, List.filled(nums.length, false), res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
@@ -359,9 +359,42 @@ comments: true
|
||||
=== "Dart"
|
||||
|
||||
```dart title="subset_sum_i_naive.dart"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:子集和 I */
|
||||
void backtrack(
|
||||
List<int> state,
|
||||
int target,
|
||||
int total,
|
||||
List<int> choices,
|
||||
List<List<int>> res,
|
||||
) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (total == target) {
|
||||
res.add(List.from(state));
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (int i = 0; i < choices.length; i++) {
|
||||
// 剪枝:若子集和超过 target ,则跳过该选择
|
||||
if (total + choices[i] > target) {
|
||||
continue;
|
||||
}
|
||||
// 尝试:做出选择,更新元素和 total
|
||||
state.add(choices[i]);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target, total + choices[i], choices, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
state.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{subsetSumINaive}
|
||||
/* 求解子集和 I(包含重复子集) */
|
||||
List<List<int>> subsetSumINaive(List<int> nums, int target) {
|
||||
List<int> state = []; // 状态(子集)
|
||||
int total = 0; // 元素和
|
||||
List<List<int>> res = []; // 结果列表(子集列表)
|
||||
backtrack(state, target, total, nums, res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
@@ -800,9 +833,45 @@ comments: true
|
||||
=== "Dart"
|
||||
|
||||
```dart title="subset_sum_i.dart"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:子集和 I */
|
||||
void backtrack(
|
||||
List<int> state,
|
||||
int target,
|
||||
List<int> choices,
|
||||
int start,
|
||||
List<List<int>> res,
|
||||
) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (target == 0) {
|
||||
res.add(List.from(state));
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
for (int i = start; i < choices.length; i++) {
|
||||
// 剪枝一:若子集和超过 target ,则直接结束循环
|
||||
// 这是因为数组已排序,后边元素更大,子集和一定超过 target
|
||||
if (target - choices[i] < 0) {
|
||||
break;
|
||||
}
|
||||
// 尝试:做出选择,更新 target, start
|
||||
state.add(choices[i]);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target - choices[i], choices, i, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
state.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{subsetSumI}
|
||||
/* 求解子集和 I */
|
||||
List<List<int>> subsetSumI(List<int> nums, int target) {
|
||||
List<int> state = []; // 状态(子集)
|
||||
nums.sort(); // 对 nums 进行排序
|
||||
int start = 0; // 遍历起始点
|
||||
List<List<int>> res = []; // 结果列表(子集列表)
|
||||
backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
@@ -1276,9 +1345,50 @@ comments: true
|
||||
=== "Dart"
|
||||
|
||||
```dart title="subset_sum_ii.dart"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:子集和 II */
|
||||
void backtrack(
|
||||
List<int> state,
|
||||
int target,
|
||||
List<int> choices,
|
||||
int start,
|
||||
List<List<int>> res,
|
||||
) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (target == 0) {
|
||||
res.add(List.from(state));
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
// 剪枝三:从 start 开始遍历,避免重复选择同一元素
|
||||
for (int 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.add(choices[i]);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target - choices[i], choices, i + 1, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
state.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{subsetSumII}
|
||||
/* 求解子集和 II */
|
||||
List<List<int>> subsetSumII(List<int> nums, int target) {
|
||||
List<int> state = []; // 状态(子集)
|
||||
nums.sort(); // 对 nums 进行排序
|
||||
int start = 0; // 遍历起始点
|
||||
List<List<int>> res = []; // 结果列表(子集列表)
|
||||
backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
Reference in New Issue
Block a user