Given a directed, acyclic graph of N nodes.  Find all possible paths from node 0 to node N-1, and return them in any order.

The graph is given as follows:  the nodes are 0, 1, ..., graph.length - 1.  graph[i] is a list of all nodes j for which the edge (i, j) exists.

Example:
Input: [[1,2], [3], [3], []]
Output: [[0,1,3],[0,2,3]]
Explanation: The graph looks like this:
0--->1
| |
v v
2--->3
There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.

Note:

  • The number of nodes in the graph will be in the range [2, 15].
  • You can print different paths in any order, but you should keep the order of nodes inside one path.

问0->n-1有几条路径

其实没什么,就是看输入有点不懂怎么建图,把图建立好,我们dfs就好了

class Solution {
public:
int Mp[][];
vector<vector<int>>ans;
vector<int>tmp;
void dfs(int n,int len){
if(len-==n){
ans.push_back(tmp);
return;
}
for(int i=;i<len;i++){
if(Mp[n][i]){
tmp.push_back(i);
dfs(i,len);
tmp.pop_back();
}
}
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
int len=graph.size();
for(int i=;i<len;i++){
for(int j=;j<graph[i].size();j++){
Mp[i][graph[i][j]]=;
}
}
tmp.clear();
ans.clear();
tmp.push_back();
dfs(,len);
return ans; }
};

最新文章

  1. Meta标签介绍
  2. 【转】70个经典的 Shell 脚本面试问题
  3. ci框架里rewrite示例
  4. 数据库 Linux下的MySQL数据库管理
  5. Yslow网站性能优化工具
  6. Hadoop:搭建hadoop集群
  7. ns3 Tutorial 中的日志模块(翻译)
  8. [转] 接触C# 反射 2
  9. Windows Linux HackMacintosh
  10. VC2008如何生成及使用DLL(完整版)
  11. C++ 学习之函数重载、基于const的重载
  12. 学习札记 ----wind7下如何安装SqlServer数据库
  13. spring boot 2.0 neo4j 使用
  14. Js 常用字符串操作 API
  15. cs231n笔记 (一) 线性分类器
  16. Emacs 设置C++代码风格
  17. HTML与XHTML的差别(转自)http://jingyan.baidu.com/article/597035521c31ed8fc007400a.html
  18. 2018.11.09 bzoj4773: 负环(倍增+floyd)
  19. Air Jordan 1 Los Primeros Will be unveiled
  20. 为arm 编译包含gd的php5

热门文章

  1. ssh框架整合其他方式(没有hibernate核心配置文件)
  2. strstr()查找函数,strchr(),strrchr(),stristr()/strpos(),strrpos()查找字符串位置
  3. ECMAScript 定义类、对象
  4. CF438D The Child and Sequence
  5. 10.model/view实例(1)
  6. C++中的内存重叠问题
  7. 常用Git命令清单
  8. enum枚举型
  9. c++特别要点:多态性与虚函数
  10. jquery easyui datagrid 多选只能获取一条数据