Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Desolve/f8e5a08fab8cce5a8c5118d8725791b7 to your computer and use it in GitHub Desktop.
Save Desolve/f8e5a08fab8cce5a8c5118d8725791b7 to your computer and use it in GitHub Desktop.
0797 All Paths From Source to Target
class Solution {
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
List<List<Integer>> res = new ArrayList<>();
helper(graph, 0, new ArrayList<>(), res);
return res;
}
private void helper(int[][] graph, int i, List<Integer> path, List<List<Integer>> res) {
path.add(i);
if (i == graph.length - 1) {
res.add(new ArrayList<Integer>(path));
} else {
for (int nxt: graph[i])
helper(graph, nxt, path, res);
}
path.remove(path.size() - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment