所有可能的路径

题目描述

官方描述(中文)
2021/08/25每日一题。

代码

菜鸡如我,只会无脑深搜。。。

class Solution 
{
public:
    void Dfs(vector<vector<int>>& graph, vector<vector<int>>&res, vector<int> vecNums, set<int>setNums, int index)
    {
        if(index == graph.size()-1)
        {
            vecNums.push_back(index);
            res.push_back(std::move(vecNums));
            return;
        }
        vecNums.push_back(index);
        setNums.insert(index);
        for(auto i:graph[index])
        {
            if(setNums.find(i)!=setNums.end())continue;
            Dfs(graph, res, vecNums, setNums, i);
        }
    }
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) 
    {
        vector<vector<int>>res;
        Dfs(graph, res, {}, {}, 0);
        return res;
    }
};

Q.E.D.