【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). ...
随机推荐
- linux 内核修炼之道——系统调用
1.问:什么是系统调用? 用户应用程序访问并使用内核所提供的各种服务的途径即是系统调用,也称系统调用接口层. 2.问:为什么需要系统调用? ① 系统调用作为内核和应用程序之间的中间层,扮演了一个桥梁角 ...
- C++ IO基础
一:c++I/O处理,按照数据输入输出的过程,形象的将其看做流.数据在流中进行传播. 所有的流有两个基类:ios和streambuf类 streambuf:提供对缓冲区的基本操作,设置缓冲区等 ios ...
- The Ninth Week (Lucklyzpp)
The Ninth Week (Lucklyzpp) 1.简述DNS服务器原理,并搭建主-辅服务器. [10:36:39 root@lucklyzpp ~]#vim /etc/named.conf ...
- airflow redis sentinel
获取master name subscribe __sentinel__:hello mysql plugin table not found mysqld --initialize-insecure ...
- 问题 B: 比大小
题目描述 给你两个很大的数,你能不能判断出他们两个数的大小呢? 比如123456789123456789要大于-123456 输入 每组测试数据占一行,输入两个不超过1000位的10进制整数a,b 数 ...
- 力扣 - 剑指 Offer 12. 矩阵中的路径
题目 剑指 Offer 12. 矩阵中的路径 思路1(回溯.DFS) 这题可以使用回溯+递归来解决,思路如下: 将二维数组的每一个元素都作为起点进行回溯查找 每次查找的时候,都有四个方向,但是上一个方 ...
- C#练习2
using System;class Class1{ public int vlaue = 0;}class Test{ static void Main() { int v1 = 0; int v2 ...
- [hdu6598]Harmonious Army
网络流建图,首先将所有价值加起来,用最小割考虑要删掉多少个价值:源点向每一个士兵连流量为x的边,士兵向汇点连流量为y的边,每一对关系间连流量为z的边,考虑有方程x1+y2+z=x2+y1+z=a+c, ...
- 力扣 - 剑指 Offer 55 - I. 二叉树的深度
题目 剑指 Offer 55 - I. 二叉树的深度 思路1(DFS) 后续遍历吧,先遍历到最深(递归到末尾返回0),然后从后面一步一步比较取大的值返回,每次返回层数都加1, 执行流程是怎样的:比如其 ...
- C/C++ Qt 自定义Dialog对话框组件应用
在上一篇博文 <C/C++ Qt 标准Dialog对话框组件应用> 中我给大家演示了如何使用Qt中内置的标准对话框组件实现基本的数据输入功能. 但有时候我们需要一次性修改多个数据,使用默认 ...