LeetCode 797. All Paths From Source to Target
题目链接:https://leetcode.com/problems/all-paths-from-source-to-target/description/
Given a directed, acyclic graph of
Nnodes. Find all possible paths from node0to nodeN-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.
看完题目描述,直觉就是DFS搜索解空间树。因为不是二维表格所以不好用DP,同时是无环图所以感觉比较像DFS。代码如下:
class Solution(object):
def allPathsSourceTarget(self, graph):
"""
:type graph: List[List[int]]
:rtype: List[List[int]]
"""
res = []
target = len(graph) - 1
self.dfs([0], res, graph[0], graph, target)
return res def dfs(self, curr_sol, res, curr_node, graph, target):
if not curr_node:
return
for nxt in curr_node:
if nxt == target:
res.append(curr_sol + [nxt])
else:
self.dfs(curr_sol+[nxt], res, graph[nxt], graph, target)
感觉是一道很标准的DFS,没有什么难点。
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】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 1059. All Paths from Source Lead to Destination
原题链接在这里:https://leetcode.com/problems/all-paths-from-source-lead-to-destination/ 题目: Given the edges ...
- [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, and re ...
- 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 re ...
- [Swift]LeetCode797. 所有可能的路径 | 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 re ...
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [LeetCode] 62. Unique Paths 唯一路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
随机推荐
- Linux环境下安装nginx
#一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩cd /usr/local/devmkdir nginxcd nginxmkdir softcd soft # ...
- 安装pipenv
首先: 安装pipenv pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pipenv 使用国内源安装pipenv 创建文件夹 mkd ...
- UML作业第三次:分析《书店图书销售管理系统》
分析图书销售管理系统 一.概览 PlantUML类图语法学习小结 <书店图书销售管理>的类图元素 绘制类图脚本程序 绘制的类图 二.PlantUML类图语法 1.类之间的关系绘制 示例: ...
- 浅析URL(接口测试必备知识)
笔者最近在开始接触接口测试的工作,接口测试必须了解http协议,但前提是必须要了解URL URL的定义 url是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标 ...
- ORA-28000: the account is locked解决
首先使用具有sysdba权限的账户登陆,如sys账户和system账户 新建一个sql窗体,并执行语句解锁被锁定的账户,如我这里sgyw账户: alter user sgyw account unlo ...
- PYTHON的前端部分
CSS部分,CSS起到修饰HTML的作用比如颜色,字体,等等. css如何和HTML共同其作用,那么CSS有几种引入HTML的方式 那么当引入了CSS如何让css对应的使相应的HTML标签上起作用,就 ...
- 网络操作基础(two)
P106 一.什么是活动目录?活动目录有哪些优点? 二.什么是域.域树.森林? 三.什么是信任?什么是域的方向及传递性? 四.如何管理活动目录的信任与站点? 解答! (一) 1.活动目录:提供了用于存 ...
- 使用DHCP动态管理主机地址
- Echart横坐标时间轴滑动
主要针对于dataZoom的使用,代码如下: option = { title: { text: '未来一周气温变化', subtext: '纯属虚构' }, tooltip: { trigger: ...
- URL编码表 Base64编码表 HTTP消息含义
URL编码表 backspace 8% A 41% a 61% § %A7 Õ %D5 tab 9% B 42% b 62% « %AB Ö %D6 linefeed %0A C 43% c ...