Skip to content

Commit 3ae0b64

Browse files
committed
add 797
1 parent 527b81b commit 3ae0b64

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

all-paths-from-source-to-target.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
// 797 https://leetcode-cn.com/problems/all-paths-from-source-to-target/
4+
// dfs
5+
func allPathsSourceTarget(graph [][]int) (ans [][]int) {
6+
path := []int{0}
7+
var dfs func(int)
8+
dfs = func(x int) {
9+
if x == len(graph)-1 {
10+
ans = append(ans, append([]int(nil), path...))
11+
return
12+
}
13+
for _, y := range graph[x] {
14+
path = append(path, y)
15+
dfs(y)
16+
path = path[:len(path)-1]
17+
}
18+
}
19+
dfs(0)
20+
return
21+
}

0 commit comments

Comments
 (0)