Skip to content

Commit

Permalink
https://leetcode.com/problems/all-paths-from-source-to-target/descrip…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
s50600822 committed Mar 17, 2024
1 parent 538166b commit d188adb
Showing 1 changed file with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

class Solution {
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
return new AbstractList<List<Integer>>() {
private List<List<Integer>> paths;

public List<Integer> get(final int index) {
if (paths == null) {
solve();
}
return paths.get(index);
}

public int size() {
if (paths == null) {
solve();
}
return paths.size();
}

private void solve() {
paths = new ArrayList<>();
dfs(
0,
graph.length - 1,
new boolean[graph.length],
new ArrayList<>() {
{
add(0);
}
});
}

private void dfs(int node, int target, boolean[] pass, List<Integer> path) {
if (node == target) {
paths.add(new ArrayList<>(path));
return;
}
for (int v : graph[node]) {
path.add(v);
dfs(v, target, pass, path);
path.remove(path.size() - 1);
}
}
};
}

public static void main(String[] args) {
//https://leetcode.com/problems/all-paths-from-source-to-target/description/
t1();
}

public static void t1() {
final Solution sol = new Solution();
final List res = sol.allPathsSourceTarget(new int[][] {
new int[]{1,2},
new int[]{3},
new int[]{3},
new int[]{0}
});
assert res.contains(Stream.of(0, 1, 3).collect(Collectors.toList()));
assert res.contains(Stream.of(0, 2, 3).collect(Collectors.toList()));
// System.out.println(res);
}



}

0 comments on commit d188adb

Please sign in to comment.