-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAllPathsFromSourceToTarget.java
41 lines (34 loc) · 1.08 KB
/
AllPathsFromSourceToTarget.java
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
37
38
39
40
41
package com.smlnskgmail.jaman.leetcodejava.medium;
import java.util.ArrayList;
import java.util.List;
// https://leetcode.com/problems/all-paths-from-source-to-target/
public class AllPathsFromSourceToTarget {
private final int[][] input;
public AllPathsFromSourceToTarget(int[][] input) {
this.input = input;
}
public List<List<Integer>> solution() {
List<List<Integer>> result = new ArrayList<>();
List<Integer> list = new ArrayList<>();
list.add(0);
fillPaths(result, list, input, 0);
return result;
}
private void fillPaths(
List<List<Integer>> result,
List<Integer> list,
int[][] graph,
int start
) {
if (start == graph.length - 1) {
result.add(new ArrayList<>(list));
} else {
for (int i = 0; i < graph[start].length; i++) {
int curr = graph[start][i];
list.add(curr);
fillPaths(result, list, graph, curr);
list.remove(list.size() - 1);
}
}
}
}