This commit is contained in:
krahets
2024-03-18 03:11:07 +08:00
parent bc0054a577
commit 54c7448946
48 changed files with 577 additions and 408 deletions

View File

@ -428,7 +428,11 @@ comments: true
```rust title="preorder_traversal_ii_compact.rs"
/* 前序遍历:例题二 */
fn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {
fn pre_order(
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
path: &mut Vec<Rc<RefCell<TreeNode>>>,
root: Option<Rc<RefCell<TreeNode>>>,
) {
if root.is_none() {
return;
}
@ -442,7 +446,7 @@ comments: true
pre_order(res, path, node.borrow().left.clone());
pre_order(res, path, node.borrow().right.clone());
// 回退
path.remove(path.len() - 1);
path.remove(path.len() - 1);
}
}
```
@ -738,7 +742,11 @@ comments: true
```rust title="preorder_traversal_iii_compact.rs"
/* 前序遍历:例题三 */
fn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {
fn pre_order(
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
path: &mut Vec<Rc<RefCell<TreeNode>>>,
root: Option<Rc<RefCell<TreeNode>>>,
) {
// 剪枝
if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {
return;
@ -753,7 +761,7 @@ comments: true
pre_order(res, path, node.borrow().left.clone());
pre_order(res, path, node.borrow().right.clone());
// 回退
path.remove(path.len() - 1);
path.remove(path.len() - 1);
}
}
```
@ -1558,7 +1566,10 @@ comments: true
}
/* 记录解 */
fn record_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>, res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>) {
fn record_solution(
state: &mut Vec<Rc<RefCell<TreeNode>>>,
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
) {
res.push(state.clone());
}
@ -1578,7 +1589,11 @@ comments: true
}
/* 回溯算法:例题三 */
fn backtrack(state: &mut Vec<Rc<RefCell<TreeNode>>>, choices: &mut Vec<Rc<RefCell<TreeNode>>>, res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>) {
fn backtrack(
state: &mut Vec<Rc<RefCell<TreeNode>>>,
choices: &mut Vec<Rc<RefCell<TreeNode>>>,
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
) {
// 检查是否为解
if is_solution(state) {
// 记录解
@ -1591,7 +1606,14 @@ comments: true
// 尝试:做出选择,更新状态
make_choice(state, choice.clone());
// 进行下一轮选择
backtrack(state, &mut vec![choice.borrow().left.clone().unwrap(), choice.borrow().right.clone().unwrap()], res);
backtrack(
state,
&mut vec![
choice.borrow().left.clone().unwrap(),
choice.borrow().right.clone().unwrap(),
],
res,
);
// 回退:撤销选择,恢复到之前的状态
undo_choice(state, choice.clone());
}

View File

@ -516,8 +516,15 @@ comments: true
```rust title="n_queens.rs"
/* 回溯算法n 皇后 */
fn backtrack(row: usize, n: usize, state: &mut Vec<Vec<String>>, res: &mut Vec<Vec<Vec<String>>>,
cols: &mut [bool], diags1: &mut [bool], diags2: &mut [bool]) {
fn backtrack(
row: usize,
n: usize,
state: &mut Vec<Vec<String>>,
res: &mut Vec<Vec<Vec<String>>>,
cols: &mut [bool],
diags1: &mut [bool],
diags2: &mut [bool],
) {
// 当放置完所有行时,记录解
if row == n {
let mut copy_state: Vec<Vec<String>> = Vec::new();
@ -562,7 +569,15 @@ comments: true
let mut diags2 = vec![false; 2 * n - 1]; // 记录次对角线上是否有皇后
let mut res: Vec<Vec<Vec<String>>> = Vec::new();
backtrack(0, n, &mut state, &mut res, &mut cols, &mut diags1, &mut diags2);
backtrack(
0,
n,
&mut state,
&mut res,
&mut cols,
&mut diags1,
&mut diags2,
);
res
}

View File

@ -354,7 +354,13 @@ comments: true
```rust title="subset_sum_i_naive.rs"
/* 回溯算法:子集和 I */
fn backtrack(mut state: Vec<i32>, target: i32, total: i32, choices: &[i32], res: &mut Vec<Vec<i32>>) {
fn backtrack(
mut state: Vec<i32>,
target: i32,
total: i32,
choices: &[i32],
res: &mut Vec<Vec<i32>>,
) {
// 子集和等于 target 时,记录解
if total == target {
res.push(state);
@ -830,7 +836,13 @@ comments: true
```rust title="subset_sum_i.rs"
/* 回溯算法:子集和 I */
fn backtrack(mut state: Vec<i32>, target: i32, choices: &[i32], start: usize, res: &mut Vec<Vec<i32>>) {
fn backtrack(
mut state: Vec<i32>,
target: i32,
choices: &[i32],
start: usize,
res: &mut Vec<Vec<i32>>,
) {
// 子集和等于 target 时,记录解
if target == 0 {
res.push(state);
@ -1344,7 +1356,13 @@ comments: true
```rust title="subset_sum_ii.rs"
/* 回溯算法:子集和 II */
fn backtrack(mut state: Vec<i32>, target: i32, choices: &[i32], start: usize, res: &mut Vec<Vec<i32>>) {
fn backtrack(
mut state: Vec<i32>,
target: i32,
choices: &[i32],
start: usize,
res: &mut Vec<Vec<i32>>,
) {
// 子集和等于 target 时,记录解
if target == 0 {
res.push(state);