75th LeetCode Weekly Contest All Paths From Source to Target
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;
}
};
75th LeetCode Weekly Contest All Paths From Source to Target的更多相关文章
- 75th LeetCode Weekly Contest Champagne Tower
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...
- 75th LeetCode Weekly Contest Smallest Rotation with Highest Score
Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1 ...
- 75th LeetCode Weekly Contest Rotate String
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- 【LeetCode】797. All Paths From Source to Target 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】797. All Paths From Source to Target
Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths fro ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
随机推荐
- linux系统 使用git图形化管理工具———gitk
运行安装命令: sudo apt-get install gitk 运行命令打开gitk : gitk
- 《Android应用性能优化》 第8章 图形
1.例子中 30个部件的xml setContentView 几乎占用了从onCreate() 到 onResume() 结束之前所有时间的99% 因为展开布局的开销很大.要尽量用不同的布局方式.比如 ...
- VS2010 rdlc报表无法显示“数据源”选项
- PHP数组函数的使用
1.array_walk($arr, $func, [$data]) 使用用户自定义的函数遍历所有的元素,返回true/false $func是一个函数名 默认会传入两个参数 第一个 $arr的值, ...
- 如何选择RDBMS关系型数据库和Nosql非关系型数据库?
RDBMS关系型数据库和Nosql非关系型数据库区别: 一.RDBMS是关系型数据库模式: 1.二维模式,由行列组成. 2.非常强调事务原子性,例如用户提出一个请求,DB完整的去执行,如果报错就全部回 ...
- [P3812][模板]线性基
解题关键:求异或最大值.线性基模板题. 极大线性无关组的概念. 异或的值域相同. #include<cstdio> #include<cstring> #include< ...
- c# 一维数组,二维数组,多维数组。
数组就是给一个变量定义多个字符,可以是string也可以是int.或者说是一组变量. 可以更加方便的操作大量数据. 数组的定义1.数组里面的内容必须是同一类型2.数据必须有长度限制 一维数组 *一.数 ...
- vray学习笔记(5)-学习资料
首先肯定是vray的官方的资料了: 一个是教程 https://docs.chaosgroup.com/display/VRAY3MAX/Tutorials 一个是帮助文件 https://docs. ...
- noi.ac day5t1 count
传送门 分析 首先一个很重要的性质是每个数至少出现一次 所以只有一个数会出现两次 我们只需要求出n+1个数选k个数的方案数再减去重复的部分即可 重复部分于两个相同数中间的距离有关,详见代码 代码 #i ...
- loj10098 分离的路径
传送门 分析 此题要先用tarjan求点双联通分量,注意在求解是要注意一条无向边只能走一次.求完之后我们发现原来的图会变成一棵树,对于 这棵树我们发现答案是(叶子节点数量+1)/2,实际便是每两个节点 ...