75th LeetCode Weekly Contest All Paths From Source to Target
2024-09-03 11:23:25
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; }
};
最新文章
- Meta标签介绍
- 【转】70个经典的 Shell 脚本面试问题
- ci框架里rewrite示例
- 数据库 Linux下的MySQL数据库管理
- Yslow网站性能优化工具
- Hadoop:搭建hadoop集群
- ns3 Tutorial 中的日志模块(翻译)
- [转] 接触C# 反射 2
- Windows Linux HackMacintosh
- VC2008如何生成及使用DLL(完整版)
- C++ 学习之函数重载、基于const的重载
- 学习札记 ----wind7下如何安装SqlServer数据库
- spring boot 2.0 neo4j 使用
- Js 常用字符串操作 API
- cs231n笔记 (一) 线性分类器
- Emacs 设置C++代码风格
- HTML与XHTML的差别(转自)http://jingyan.baidu.com/article/597035521c31ed8fc007400a.html
- 2018.11.09 bzoj4773: 负环(倍增+floyd)
- Air Jordan 1 Los Primeros Will be unveiled
- 为arm 编译包含gd的php5
热门文章
- ssh框架整合其他方式(没有hibernate核心配置文件)
- strstr()查找函数,strchr(),strrchr(),stristr()/strpos(),strrpos()查找字符串位置
- ECMAScript 定义类、对象
- CF438D The Child and Sequence
- 10.model/view实例(1)
- C++中的内存重叠问题
- 常用Git命令清单
- enum枚举型
- c++特别要点:多态性与虚函数
- jquery easyui datagrid 多选只能获取一条数据