题目:
代码(首刷看解析 2024年3月18日):
class Solution {
public:
vector<vector<int>> result;
vector<int> paths;
void dfs(vector<vector<int>>& graph, int row)
{
if (row == graph.size() - 1)
{
result.push_back(paths);
return;
}
for (int i = 0; i < graph[row].size(); ++i)
{
paths.emplace_back(graph[row][i]);
dfs(graph, graph[row][i]);
paths.pop_back();
}
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
int n = graph.size();
paths.push_back(0);
dfs(graph, 0);
return result;
}
};