mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0797.所有可能的路径 Go写法
This commit is contained in:
@ -217,6 +217,34 @@ class Solution:
|
|||||||
self.path.pop() # 回溯
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
|
Reference in New Issue
Block a user