Skip to content

Commit 493085f

Browse files
authored
Create 797.cpp
1 parent 66bab99 commit 493085f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

797/797.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> dfs(vector<vector<int>>& adj, int x, int n) {
4+
// base
5+
if (x == n - 1) {
6+
return {{x}};
7+
}
8+
9+
// main
10+
vector<vector<int>> ans;
11+
for (auto v : adj[x]) {
12+
// x -> v
13+
vector<vector<int>> paths = dfs(adj, v, n);
14+
for (auto subpath : paths) {
15+
subpath.push_back(x);
16+
ans.push_back(subpath);
17+
}
18+
}
19+
20+
return ans;
21+
}
22+
23+
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
24+
int n = graph.size();
25+
vector<vector<int>> paths = dfs(graph, 0, n);
26+
27+
for(auto& path: paths){
28+
reverse(path.begin(), path.end());
29+
}
30+
return paths;
31+
}
32+
};

0 commit comments

Comments
 (0)