题目链接: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.

看完题目描述,直觉就是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的更多相关文章

  1. 【LeetCode】797. All Paths From Source to Target 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  2. 【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 ...

  3. 【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 ...

  4. LeetCode 1059. All Paths from Source Lead to Destination

    原题链接在这里:https://leetcode.com/problems/all-paths-from-source-lead-to-destination/ 题目: Given the edges ...

  5. [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 ...

  6. 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 ...

  7. [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 ...

  8. 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). ...

  9. [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 ...

随机推荐

  1. 3.7 unittest之断言

    3.7 unittest之断言 前言在测试用例中,执行完测试用例后,最后一步是判断测试结果是pass还是fail,自动化测试脚本里面一般把这种生成测试结果的方法称为断言(assert).用unitte ...

  2. temporal credit assignment in reinforcement learning 【强化学习 经典论文】

    Sutton 出版论文的主页: http://incompleteideas.net/publications.html Phd  论文:   temporal credit assignment i ...

  3. vscode C++开发环境配置教程(教你如何用vscode写C++)

    用了一段时间的cb,Devc++,但一直感觉cb的高亮太差,而Devc++使用体验差(尤其是代码补全功能),换过vs2017,但是由于其太大了,卡顿十分明显,所以最终选择了vscode这款轻量级编译器 ...

  4. Gym101889J. Jumping frog(合数分解+环形dp预处理)

    比赛链接:传送门 题目大意: 一只青蛙在长度为N的字符串上跳跃,“R”可以跳上去,“P”不可以跳上去. 字符串是环形的,N-1和0相连. 青蛙的跳跃距离K的取值范围是[1, N-1],选定K之后不可改 ...

  5. java项目---用java重命名文件(1星)

    package Demo; import java.io.File; public class FileRename { public static boolean Rename(String old ...

  6. HOMEWORK1

    回顾你过去将近3年的学习经历 当初你报考的时候是真正喜欢计算机这个专业吗? 当初报考的时候是选择英语和计算机专业,报英语那个学校没去上,就来学了计算机,对计算机专业的感觉介于喜欢和热爱之间,就是说还是 ...

  7. Python基础:三、Python的解释器

    当我们编写python代码的时候,我们得到的是一个包含python代码的以.py为拓展名的文本文件,要运行代码,就需要python解释器去执行.py文件. 由于整个python语言从规范到解释器都是开 ...

  8. MFC中给各个控件填充背景颜色的方法

    1.给程序设置大背景色,在OnPaint()函数中添加如下代码: CRect rect; CPaintDC dc(this); GetClientRect(rect); dc.FillSolidRec ...

  9. 自定义事件——Event和CustomEvent

    之前在学习自定义事件时,在MDN的Event.initEvent()页面顶端有写:该特性已从Web标准中删除,虽然一些浏览器目前仍然支持它,但也许会在未来的某个时间停止支持,请尽量不要使用该特性. 作 ...

  10. 左耳听风-ARTS-第2周(2019/3/31-2019/4/6)

    Algorithm 验证括号题(https://leetcode.com/problems/valid-parentheses/).这道题在极客时间上覃超的<算法面试通关40讲>(http ...