From 076d87605e29d2474f89d2e7312d2a9a2fc69818 Mon Sep 17 00:00:00 2001 From: Shixiaocaia <68102662+shixiaocaia@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:26:53 +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=20Go=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0797.所有可能的路径.md | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0797.所有可能的路径.md b/problems/0797.所有可能的路径.md index ec8288c6..5c209f60 100644 --- a/problems/0797.所有可能的路径.md +++ b/problems/0797.所有可能的路径.md @@ -217,6 +217,34 @@ class Solution: self.path.pop() # 回溯 ``` +### Go + +```go +func allPathsSourceTarget(graph [][]int) [][]int { + result := make([][]int, 0) + + var trace func(path []int, step int) + trace = func(path []int, step int){ + // 从0遍历到length-1 + if step == len(graph) - 1{ + tmp := make([]int, len(path)) + copy(tmp, path) + result = append(result, tmp) + return + } + + for i := 0; i < len(graph[step]); i++{ + next := append(path, graph[step][i]) + trace(next, graph[step][i]) + } + } + // 从0开始,开始push 0进去 + trace([]int{0}, 0) + return result +} + +``` +