class Solution {
public:
vector<vector<int>> res;
vector<int> temp;
void dfs(vector<vector<int>> graph, int x){
if(x == graph.size() - 1){
res.push_back(temp);
return;
}
for(int i = 0; i < graph[x].size(); ++i){
temp.push_back(graph[x][i]);
dfs(graph, graph[x][i]);
temp.pop_back(); //回溯
}
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
temp.push_back(0);
dfs(graph, 0);
return res;
}
};
LeetCode 797. 所有可能的路径
最新推荐文章于 2024-11-05 21:58:11 发布