Update 0797.所有可能的路径.md about rust

This commit is contained in:
fwqaaq
2023-08-21 18:10:48 +08:00
committed by GitHub
parent 49e1ab2b57
commit 264b72f3c6

View File

@ -217,7 +217,29 @@ class Solution:
self.path.pop() # 回溯
```
### Rust
```rust
impl Solution {
pub fn all_paths_source_target(graph: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let (mut res, mut path) = (vec![], vec![0]);
Self::dfs(&graph, &mut path, &mut res, 0);
res
}
pub fn dfs(graph: &Vec<Vec<i32>>, path: &mut Vec<i32>, res: &mut Vec<Vec<i32>>, node: usize) {
if node == graph.len() - 1 {
res.push(path.clone());
return;
}
for &v in &graph[node] {
path.push(v);
Self::dfs(graph, path, res, v as usize);
path.pop();
}
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">