3.797.所有可能的路径
·
思路:
这道题和上一道题一模一样。但是格式是力扣格式而非acm模式
力扣模式有区别。它的图的定义像是邻接表的定义。所以再遍历的时候有区别。
它是以graph[i][j]为终点,而非以j为终点。所以更像是邻接表的写法。
储备:
问题重点:
最后:
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
void dfs(vector<vector<int>>& graph,int s) {
if (s==graph.size()-1) {
res.push_back(path);
return ;
}
for (int i=0;i<graph[s].size();i++) {//遍历其它点
path.push_back(graph[s][i]);
dfs(graph,graph[s][i]);
path.pop_back();
}
return ;
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
int n=graph.size();
//从起点开始遍历。
path.push_back(0);
dfs(graph,0);
return res;
}
};
更多推荐
所有评论(0)