思路:

这道题和上一道题一模一样。但是格式是力扣格式而非acm模式

力扣模式有区别。它的图的定义像是邻接表的定义。所以再遍历的时候有区别。

它是以graph[i][j]为终点,而非以j为终点。所以更像是邻接表的写法。

储备:

797. 所有可能的路径 - 力扣(LeetCode)

问题重点:

最后:

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;
    }
};

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐