Problem 1 All Path From Source to Target
https://leetcode.com/problems/all-paths-from-source-to-target/
Last updated
https://leetcode.com/problems/all-paths-from-source-to-target/
Last updated
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
List<List<Integer>> result = new ArrayList<>();
if (graph == null || graph.length == 0) {
return result;
}
List<Integer> current = new ArrayList<>();
boolean[] visited = new boolean[graph.length];
int curNode = 0;
dfs(curNode, graph, result, current, visited);
return result
}
// recursion tree implementation
// int curNode: 当前我站在哪个点上
private void dfs(int curNode, int[][] graph, List<List<Intger>> result, List<Integer> current, boolean[] visited) {
if (curNode == graph.length - 1) {
current.add(curNode);
result.add(new ArrayList<>(current));
current.remove(current.size() - 1);
return;
}
current.add(curNode);
visited[curNode] = true;
for (int nei: graph[curNode]) {
if (visited[nei]) {
continue;
}
dfs(nei, graph, result, current, visited);
}
current.remove(current.size() - 1);
visited[curNode] = false;
}
// 这题可以不mark visited ,因为这里是directed acyclic graph(DAG):题目已经明确说明了是一个有向无环图,也就是说我们沿着方向走,只要没有自环是不会走出环的。
// 不过从物理意义上来说,应该是mark visited
class Solution {
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
List<List<Integer>> ret = new ArrayList<>();
if (graph == null) return ret;
List<Integer> curResult = new ArrayList<>();
curResult.add(0);
int[] visited = new int[graph.length];
visited[0] = 1;
helper(graph, curResult, visited, ret, 0);
return ret;
}
private void helper(int[][] graph, List<Integer> curResult, int[] visited, List<List<Integer>> ret, int curPosition) {
// base case
if (visited[visited.length - 1] == 1) {
ret.add(new ArrayList<>(curResult));
return;
}
// recursion rule
for (int possVisit : graph[curPosition]) {
if (visited[possVisit]!= 1) {
curResult.add(possVisit);
visited[possVisit] = 1;
helper(graph, curResult, visited, ret, possVisit);
curResult.remove(curResult.size() - 1);
visited[possVisit] = 0;
}
}
}
}