zl程序教程

您现在的位置是:首页 >  其他

当前栏目

leetcode 797 所有可能的路径

LeetCode 所有 路径 可能
2023-09-27 14:29:24 时间

所有可能的路径

在这里插入图片描述

class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;
    void dfs(vector<vector<int>>& graph , int indnx)
    {
        if(indnx == graph.size()-1) 
        {
            path.push_back(graph.size()-1);
            result.push_back(path);
            path.pop_back();
            return;
        }

        for(int i=0 ; i<graph[indnx].size() ;i++)
        {
            path.push_back(indnx);
            dfs(graph,graph[indnx][i]);
            path.pop_back();
        }
        return;
    }
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        dfs(graph,0);
        return result;
    }
};