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 ...
随机推荐
- ssm登录与退出
ssm整合比较好的实例 http://how2j.cn/k/ssm/ssm-tutorial/1137.html?tid=77#nowhere ssm登录后台用户检测(此实例注销有问题):http:/ ...
- 忽略SIGPIPE信号
#include <stdlib.h> #include <sys/signal.h> void SetupSignal() { struct sigaction sa; // ...
- 小伙 zwfw-new.hunan.gov.cn.iname.damddos.com [222.240.80.52]
由于这个应用出问题非常影响用户体验:于是立马让运维保留现场 dump 线程和内存同时重启应用,还好重启之后恢复正常.于是开始着手排查问题.
- vim 加密(crypt)文本文档
算法 vim7.3版本支持两种加密方式——PKzip算法(已知有缺陷的).Blowfish算法(从7.3版本开始支持).Blowfish2算法(从7.4.399版本开始支持)而vim -x 默认采用P ...
- java数组的定义
class ArrayDome { public static void main(String[] args) { //元素类型[] 数组名 创建一个 元素类型[元素个数或数组长度] /* 需要一个 ...
- Linux下的tar压缩解压缩命令详解(转)
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- 腾讯广告联盟 Android SDK(广点通)demo的使用方式
1. 下载示例文件. 2. 解压之后的目录: 3. 使用android studio,选择import project,导入如图所示文件夹: 4. 重点来了,由于官方demo的上传时间很久远(大概是上 ...
- myEclipse出现cannot paste the clipboard contents into the selected elements报错
导入jar包报错,cannot paste the clipboard contents into the selected elements,查阅资料让重新打开工程,但依然报错. 最后在本地路径复制 ...
- Linux 系统的用户和组
目录 1. 用户及组相关文件 2. 用户相关查询 2.1 直接通过cat文件查看用户及组文件内容 2.2 使用下面查询命令查看 3. 使用操作命令修改用户及组相关文件 3.1 专有编辑命令(仅限高级用 ...
- selenium_Python3_邮箱登录:动态元素定位
这里的关键是动态frame定位: 其他元素定位不用多说,常规操作. 不过需要注意加上这个: from selenium.webdriver.remote.webelement import WebEl ...