【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 from node 0 to node n - 1 and return them in any order.The graph is given as follows: graph[i] is a list of all nodes you can visit from node i (i.e., there is a directed edge from node i to node graph[i][j]).
Example 1:

Input: graph = [[1,2],[3],[3],[]]
Output: [[0,1,3],[0,2,3]]
Explanation: There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
这道题的需求就是从0节点开始依次遍历到最后一个节点,并求出所有的路径,为了求出所有的路径一般选择递归的方式,而且这题的数据没有节点遍历的死循环(即0->1 1->0),所有节点都会指向下一个不重复的节点,这样就需要额外
的判断了。
递归终止条件就是检索到最后一个节点,然后把路径记录下来。
class Solution {
private:
int end=0;
public:
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
// 记录所有的paths 利用递归
// 所有路径都从0节点开始
// 在每个节点中循环去寻找下一个节点 最终如果找到end节点 则退出
end=graph.size()-1;
vector<vector<int>> res;
vector<int> dp;
digui(0,res,dp,graph);
return res;
}
void digui(int point,vector<vector<int>>&res,vector<int>dp,vector<vector<int>>& graph)
{
dp.push_back(point); //填充当前节点
if(point==end){
res.push_back(dp);
return;
}
vector<int> nums=graph[point];
for(auto nn:nums){
digui(nn,res,dp,graph);
}
return;
}
};
【leetcode】797. All Paths From Source to Target的更多相关文章
- 【LeetCode】797. All Paths From Source to Target 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】62. Uniqe Paths
题目 https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m ...
- LeetCode 797. All Paths From Source to Target
题目链接:https://leetcode.com/problems/all-paths-from-source-to-target/description/ Given a directed, ac ...
- 【LeetCode】980. Unique Paths III解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】63. Unique Paths II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【LeetCode】62. Unique Paths
Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...
- 【LeetCode】062. Unique Paths
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
随机推荐
- hdu 5170 GTY's math problem(水,,数学,,)
题意: 给a,b,c,d. 比较a^b和c^d的大小 思路: 比较log(a^b)和log(c^d)的大小 代码: int a,b,c,d; int main(){ while(scanf(" ...
- openssh 7.4 升级 8.3
1.删除旧版本(如果是远程连接升级,不能卸载旧版本,否则连接会断开.安装8.3完也不能卸载7.4,否则要到服务器直连重新安装8.3.) # rpm -qa |grep openssh #rpm -e ...
- 剖析虚幻渲染体系(12)- 移动端专题Part 1(UE移动端渲染分析)
目录 12.1 本篇概述 12.1.1 移动设备的特点 12.2 UE移动端渲染特性 12.2.1 Feature Level 12.2.2 Deferred Shading 12.2.3 Groun ...
- 我罗斯方块最终篇(Player类、Game类)
我罗斯方块最终篇(Player类.Game类) |--------------------项目GitHub地址--------------------| 目录 我罗斯方块最终篇(Player类.Gam ...
- More Effective C++笔记(二)
三.异常 条款9:利用destructor避免泄露资源 把资源封装在对象内,通常可以在exception出现时避免资源泄露 条款10:在构造函数内阻止资源泄露 C++仅仅能删除被完全构造的对象(ful ...
- NOIP模拟99(多校31)
T1 法阵 解题思路 原题3100,张口放 T1(出题人原话) 思维题,合法的情况其实就是上下两个梯形拼起来的样子. 他们的边界都是在 \(i\) 轴上面,但是不能相交. 于是我们可以尝试两者相交的纵 ...
- 新技能GET!在前端表格中花式使用异步函数的奥义
背景 60年代时,操作系统中独立运行的单元通常是进程.但随着计算机技术的发展,人们发现在进程运行过程中,创建.撤销与切换都要花费较大的时空开销. 到了80年代为了解决这一问题,出现了更小的独立运行基本 ...
- 【拥抱元宇宙】创建你的第一个Unity程序HelloWorld,并发布
第一个Unity程序--Hello World. 1.需要先下载一个Unity Hub,以及安装Unity编辑器.Unity Hub需要登陆,激活码可以选择个人用户,免费的.免费的无法改变启动画面,其 ...
- Github树型插件--Octotree
octotree 是一款chrome插件,用于将 GitHub 项目代码以树形格式展示,而且在展示的列表中,我们可以下载指定的文件,而不需要下载整个项目. 官网地址:https://www.octot ...
- WebRTC与CSS滤镜(CSS filter)
我们知道了如何使用WebRTC打开摄像头,可以截取视频帧并且用canvas显示出来. 本文将滤镜与视频结合.给视频加上一层滤镜.主要使用到的是filter属性. canvas与滤镜 先来看filter ...