【LeetCode】797. All Paths From Source to Target 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/all-paths-from-source-to-target/description/
题目描述
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.
题目大意
给出了一个有向无环图,求从起点到终点的所有路径。图的表示方法是,共有n个节点,其数字分别为0…n-1,给出的图graph的每个位置对应的是第i个节点能到达的下一个节点的序号位置。比如题中graph[0] = [1,2]表示图的起点0指向了1,2两个节点。
解题方法
回溯法
经典的dfs的题目啊,第一遍没做这个题的原因是没看懂题目。。
直接使用dfs的模板公式即可,要注意的是给出的path默认就带着起点0,每次添加的是下个节点n不是当前节点pos。停止的条件是 pos == len(graph) - 1。
代码:
class Solution(object):
def allPathsSourceTarget(self, graph):
"""
:type graph: List[List[int]]
:rtype: List[List[int]]
"""
res = []
self.dfs(graph, res, 0, [0])
return res
def dfs(self, graph, res, pos, path):
if pos == len(graph) - 1:
res.append(path)
return
else:
for n in graph[pos]:
self.dfs(graph, res, n, path + [n])
二刷的时候对这个题写法更简单了,因为题目给出的是有向无环图,到达根节点之后可以继续搜索,但是不可能再次到达终点了。
class Solution(object):
def allPathsSourceTarget(self, graph):
"""
:type graph: List[List[int]]
:rtype: List[List[int]]
"""
res = []
self.dfs(graph, 0, len(graph) - 1, res, [0])
return res
def dfs(self, graph, start, end, res, path):
if start == end:
res.append(path)
for node in graph[start]:
self.dfs(graph, node, end, res, path + [node])
在Python代码里面可以随便就生成了新的列表,导致回溯过程看不清楚,但是C++版本的回溯法因为只用了一个res和一个path,所以回溯过程看的很清楚。
class Solution {
public:
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
vector<int> path;
path.push_back(0);
dfs(graph, 0, graph.size() - 1, path);
return res;
}
private:
vector<vector<int>> res;
void dfs(vector<vector<int>>& graph, int start, int end, vector<int> path) {
if (start == end) {
res.push_back(path);
} else {
for (int node : graph[start]) {
path.push_back(node);
dfs(graph, node, end, path);
path.pop_back();
}
}
}
};
日期
2018 年 3 月 20 日 ————阳光明媚~
2018 年 12 月 2 日 —— 又到了周日
【LeetCode】797. All Paths From Source to Target 解题报告(Python & C++)的更多相关文章
- LeetCode 797. All Paths From Source to Target
题目链接:https://leetcode.com/problems/all-paths-from-source-to-target/description/ Given a directed, ac ...
- 【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】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, a ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)
[LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...
随机推荐
- mysql—MySQL数据库中10位或13位时间戳和标准时间相互转换
1.字符串时间转10位时间戳 select FLOOR(unix_timestamp(create_time)) from page; #create_time为字段名 page为表名 eg:sele ...
- Excel-转换单元格格式的函数或“方法”汇总
14.转换单元格格式的函数或"方法"汇总 =value(单元格) #转换为数值 =A1&"" #转换A1为文本 = ...
- 判断是否有重复,判断字符串是否有重复汉字【c#】
string corn = "公司"; int n = 0; if (tbCorporateName.Text.IndexOf(corn) > -1) { string co ...
- C++中union相关
前两天做阿里笔试遇到一个选择题题目大概是 #include <iostream> #include <stdlib.h> using namespace std; union ...
- Android中的性能优化
由于手机硬件的限制,内存和CPU都无法像pc一样具有超大的内存,Android手机上,过多的使用内存,会容易导致oom,过多的使用CPU资源,会导致手机卡顿,甚至导致anr.我主要是从一下几部分进行优 ...
- 编译安装nginx 1.16
准备源码包,并解压,创建nginx用户 [root@slave-master ~]# tar xf nginx-1.16.0.tar.gz [root@slave-master ~]# useradd ...
- 重量级&轻量级
重量级 就是说包的大小,还有就是与个人项目的耦合程度,重量级的框架与项目耦合程度大些 代表EJB容器的服务往往是"买一送三",不要都不行 轻量级 就是相对较小的包,当然与项目的耦合 ...
- SSM框架整合后使用pagehelper实现分页功能
一.导入pagehelper-5.1.10.jar和jsqlparser-3.1.jar两个jar包 二.配置pagehelper 2.1 在mybatis配置文件中配置 <plugins> ...
- 注册页面css版本
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- MySQL-核心技术
1.基本查询语句 1.1使用select 语句查询一个数据表 select * from user; 1.2 查询表中的一列或多列 select id,ixdh from user; 1.3从一个表或 ...