From 264b72f3c63c937e27262b7e92fba027eed6e93c Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 18:10:48 +0800 Subject: [PATCH] =?UTF-8?q?Update=200797.=E6=89=80=E6=9C=89=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E7=9A=84=E8=B7=AF=E5=BE=84.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0797.所有可能的路径.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0797.所有可能的路径.md b/problems/0797.所有可能的路径.md index ec8288c6..9d14bd7c 100644 --- a/problems/0797.所有可能的路径.md +++ b/problems/0797.所有可能的路径.md @@ -217,7 +217,29 @@ class Solution: self.path.pop() # 回溯 ``` +### Rust +```rust +impl Solution { + pub fn all_paths_source_target(graph: Vec>) -> Vec> { + let (mut res, mut path) = (vec![], vec![0]); + Self::dfs(&graph, &mut path, &mut res, 0); + res + } + + pub fn dfs(graph: &Vec>, path: &mut Vec, res: &mut Vec>, 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(); + } + } +} +```