-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAllPossiblePaths.java
More file actions
36 lines (32 loc) · 914 Bytes
/
AllPossiblePaths.java
File metadata and controls
36 lines (32 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package recursion_and_dynamic_programming;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: Wenhang Chen
* @Description:
* @Date: Created in 21:58 7/31/2020
* @Modified by:
*/
public class AllPossiblePaths {
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
return solve(graph, 0);
}
public List<List<Integer>> solve(int[][] graph, int node) {
int N = graph.length;
List<List<Integer>> ans = new ArrayList<>();
if (node == N - 1) {
List<Integer> path = new ArrayList<>();
path.add(N - 1);
ans.add(path);
return ans;
}
for (int nei : graph[node]) {
// fox循环还能这么用
for (List<Integer> path : solve(graph, nei)) {
path.add(0, node);
ans.add(path);
}
}
return ans;
}
}