【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). ...
随机推荐
- eclipse javaEE版下载过程中选择镜像(Select Another Mirror)无反应解决办法,附带eclipse javaEE版下载教程。
1.eclipse javaEE版下载过程中选择镜像(Select Another Mirror)无反应 (复制该网址下载即可 https://mirrors.neusoft.edu.cn/eclip ...
- 前端面试手写代码——call、apply、bind
1 call.apply.bind 用法及对比 1.1 Function.prototype 三者都是Function原型上的方法,所有函数都能调用它们 Function.prototype.call ...
- Ubuntu安装数据库
1.通过命令行安装:sudo apt-get install mysql-client mysql-server 2.安装过程中输入数据库密码("123456",root) 3.使 ...
- NSFOCUS建议您采取以下措施以降低威胁: * 修改源代码或者配置文件改变SSH服务的缺省banner。
老扫漏洞出来要整改 1.在 /etc/下创建一个文件 ssh_banner_change,在文件中输入内容,如:welcome! 2.修改/etc/ssh/sshd_config 文件,添加以下内容: ...
- Jenkins file一行代码部署.NET程序到K8S
什么是Jenkins共享库 随着微服务的增多,每个项目的都需要pipline文件,这样的话Pipeline代码冗余度高,并且pipeline的功能越来越复杂. jenkins可以使用Shared Li ...
- Linux常用命令和快捷键整理:(1)常用命令
前言: Linux常用快捷键和基本命令整理,先上思维导图: 1.ls命令 就是list的缩写,通过ls 命令不仅可以查看linux文件夹包含的文件,而且可以查看文件权限(包括目录.文件夹.文件权限) ...
- Kali-Linux 2020如何设置中文
话不多说,直接上步骤 首先,想要修改系统默认语言普通用户是办不到的,这个时候就需要切换为root用户在终端输入 sudo su(切换用户指令,后面不加用户名就默认切换为root) 输入管理员密码后就像 ...
- springboot注解之容器功能
添加组件 @Configuration.@Bean //以swagger为例 @Configuration(proxyBeanMethods = false) @EnableSwagger2 //使用 ...
- 数据库炸了----我就重启了一下啊(Communications link failure)
重启数据库后,数据库大部分时间连不上了:连续请求不会报错,请求间隔时间稍微长一点就会报错报错如图: com.mysql.cj.jdbc.exceptions.CommunicationsExcepti ...
- 『与善仁』Appium基础 — 9、补充:C/S架构和B/S架构说明
目录 1.C/S架构和B/S架构概念 2.C/S结构与B/S架构的区别 3.C/S架构和B/S架构优点和缺点 (1)B/S模式的优点和缺点: (2)C/S模式的优点和缺点: 1.C/S架构和B/S架构 ...