-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
36 lines (34 loc) 路 815 Bytes
/
Copy pathSolution.java
File metadata and controls
36 lines (34 loc) 路 815 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
class Solution {
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
List<Integer> visited = new ArrayList<>();
for (int i = 0; i < graph.length; i++) { visited.add(0); }
return dfs(
0,
graph,
new ArrayList<Integer>(),
visited
);
}
public List<List<Integer>> dfs(int c, int[][] graph, List<Integer> acc, List<Integer> visited) {
List<List<Integer>> res = new ArrayList<>();
if (visited.get(c) == 0) {
visited.set(c, 1);
acc.add(c);
if (c == graph.length - 1) {
res.add(acc);
return res;
}
for (int i = 0; i < graph[c].length; i++) {
if (visited.get(graph[c][i]) == 0) {
res.addAll(dfs(
graph[c][i],
graph,
new ArrayList<Integer>(acc),
new ArrayList<Integer>(visited)
));
}
}
}
return res;
}
}