-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
Copy pathAllPathFromSourceToTarget.java
39 lines (34 loc) · 1 KB
/
AllPathFromSourceToTarget.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
package problems.medium;
import java.util.ArrayList;
import java.util.List;
/**
* Why Did you create this class? what does it do?
*/
public class AllPathFromSourceToTarget {
public static void main(String[] args) {
System.out.println(new AllPathFromSourceToTarget().allPathsSourceTarget(new int[][] {
{ 1, 2 },
{ 3 },
{ 3 },
{},
}));
}
public List<List<Integer>> allPathsSourceTarget(int[][] a) {
List<List<Integer>> list = new ArrayList<>();
List<Integer> ll = new ArrayList<>();
ll.add(0);
go(a, 0, list, ll);
return list;
}
void go(int[][] a, int i, List<List<Integer>> list, List<Integer> ll) {
if (i == a.length - 1) {
list.add(ll);
return;
}
for (int j = 0; j < a[i].length; j++) {
List<Integer> l = new ArrayList<>(ll);
l.add(a[i][j]);
go(a, a[i][j], list, l);
}
}
}