Skip to content

Commit b042b59

Browse files
committed
AllPathsFromSourceToTarget797
1 parent 764fbc0 commit b042b59

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@
5353
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
5454

5555

56-
# Total: 429
56+
# Total: 430
5757

5858
| Easy | Medium | Hard | - |
5959
|:-------:|:-------:|:----:|:-:|
60-
| 116 | 230 | 69 | 4 |
60+
| 116 | 231 | 69 | 4 |
6161

6262

6363
| Question | Solution | Difficulty |
@@ -467,6 +467,7 @@
467467
| [771. Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/JewelsAndStones771.java) | Easy |
468468
| [777. Swap Adjacent in LR String](https://leetcode.com/problems/swap-adjacent-in-lr-string/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/SwapAdjacentInLRString777.java) | Medium |
469469
| [779. K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/KthSymbolInGrammar779.java) | Medium |
470+
| [797. All Paths From Source to Target](https://leetcode.com/problems/all-paths-from-source-to-target/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/AllPathsFromSourceToTarget797.java) | Medium |
470471
| [783. Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MinimumDistanceBetweenBSTNodes783.java) | Easy |
471472
| [802. Find Eventual Safe States](https://leetcode.com/problems/find-eventual-safe-states/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/FindEventualSafeStates802.java) | Medium |
472473
| [819. Most Common Word](https://leetcode.com/problems/most-common-word/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MostCommonWord819.java) | Easy |
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Given a directed, acyclic graph of N nodes. Find all possible paths from
3+
* node 0 to node N-1, and return them in any order.
4+
*
5+
* The graph is given as follows: the nodes are0, 1, ..., graph.length - 1.
6+
* graph[i] is a list of all nodes j for which the edge (i, j) exists.
7+
*
8+
* Example:
9+
* Input: [[1,2], [3], [3], []]
10+
* Output: [[0,1,3],[0,2,3]]
11+
* Explanation: The graph looks like this:
12+
* 0--->1
13+
* | |
14+
* v v
15+
* 2--->3
16+
* There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
17+
*
18+
* Note:
19+
* The number of nodes in the graph will be in the range [2, 15].
20+
* You can print different paths in any order, but you should keep the order
21+
* of nodes inside one path.
22+
*/
23+
24+
public class AllPathsFromSourceToTarget797 {
25+
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
26+
List<List<Integer>> res = new ArrayList<>();
27+
if (graph == null || graph.length == 0) return res;
28+
int N = graph.length;
29+
backtrace(graph, 0, N-1, new ArrayList<>(), res, N);
30+
return res;
31+
}
32+
33+
private void backtrace(int[][] graph, int current, int dest, List<Integer> path, List<List<Integer>> res, int N) {
34+
if (current == dest) {
35+
path.add(current);
36+
res.add(new ArrayList<>(path));
37+
path.remove(path.size() - 1);
38+
return;
39+
}
40+
41+
path.add(current);
42+
for (int child: graph[current]) {
43+
backtrace(graph, child, N-1, path, res, N);
44+
}
45+
path.remove(path.size() - 1);
46+
}
47+
48+
49+
public List<List<Integer>> allPathsSourceTarget2(int[][] graph) {
50+
List<List<Integer>> res = new ArrayList<>();
51+
if (graph == null || graph.length == 0) return res;
52+
int N = graph.length;
53+
List<Integer> path = new ArrayList<>();
54+
path.add(0);
55+
backtrace2(graph, 0, N-1, path, res, N);
56+
return res;
57+
}
58+
59+
private void backtrace2(int[][] graph, int current, int dest, List<Integer> path, List<List<Integer>> res, int N) {
60+
if (current == dest) {
61+
res.add(new ArrayList<>(path));
62+
return;
63+
}
64+
65+
for (int child: graph[current]) {
66+
path.add(child);
67+
backtrace2(graph, child, N-1, path, res, N);
68+
path.remove(path.size() - 1);
69+
}
70+
}
71+
72+
}

0 commit comments

Comments
 (0)