-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllPathsFromSourceToTarget.java
40 lines (34 loc) · 1.45 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
package solutions;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
// [Problem] https://leetcode.com/problems/all-paths-from-source-to-target
class AllPathsFromSourceToTarget {
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
List<List<Integer>> allPaths = new ArrayList<>();
List<Integer> path = new LinkedList<>();
findPath(0, path, allPaths, graph);
return allPaths;
}
private void findPath(int currentPoint, List<Integer> path, List<List<Integer>> allPaths, int[][] graph) {
path = new ArrayList<>(path);
path.add(currentPoint);
if (currentPoint == graph.length - 1) {
allPaths.add(path);
return;
}
int[] nextPoints = graph[currentPoint];
for (int nextPoint: nextPoints) {
findPath(nextPoint, path, allPaths, graph);
}
}
// Test
public static void main(String[] args) {
AllPathsFromSourceToTarget solution = new AllPathsFromSourceToTarget();
int[][] input = {{4, 3, 1}, {3, 2, 4}, {3}, {4}, {}};
List<List<Integer>> expectedOutput = List.of(List.of(0, 4), List.of(0, 3, 4), List.of(0, 1, 3, 4), List.of(0, 1, 2, 3, 4), List.of(0, 1, 4));
List<List<Integer>> actualOutput = solution.allPathsSourceTarget(input);
System.out.println(actualOutput);
System.out.println("Test passed? " + expectedOutput.equals(actualOutput));
}
}