Created
May 30, 2023 14:44
-
-
Save Desolve/f8e5a08fab8cce5a8c5118d8725791b7 to your computer and use it in GitHub Desktop.
0797 All Paths From Source to Target
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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