题目链接: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. AutomaticReferenceCounting.html#runtime-support

    https://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime-support Runtime support This sec ...

  2. hadoop day 7

    1.storm概述 应用于实时的流式计算,结合消息队列和数据库进行使用. Spouts:拓扑的消息源 Bolts:拓扑的处理逻辑单元,每个bolt可以在集群当中多实例的并发执行 tuple:消息元组, ...

  3. ckeditor_配置 修改工具栏段落的标签和在文中的格式

    在默认的工具栏中自带一个格式的选项,可以编辑文字的段落属性,将文字转换为标题.ckeditor中提供了方法可以对这个标签进行修改. 正在做的项目中不叫标题1标题2,叫章.节.条... . 需要进行配置 ...

  4. HBase学习笔记1 - 如何编写高性能的客户端Java代码

    转载请标注原链接:http://www.cnblogs.com/xczyd/p/5577124.html 客户在使用HBase的时候,经常会抱怨说写入太慢,并发上不去等等.从前我遇到这种情况,一般都二 ...

  5. jQuery基础之二

    jQuery基础之二   jQuery的中文文档十分详细,并且权威.W3C的jQuery文档更加简单实用. jQuery 1.9.0 jQuery中文文档W3C的jQuery文档 jQuery操作DO ...

  6. c# excel xls保存

    public HSSFWorkbook Excel_Export(DataTable query,string title,int[] rowweight,string[] rowtitle) { H ...

  7. mysql中的备份(backup)和恢复(recovery)

    (一)备份类型(backup type) 物理和逻辑备份(Physical Versus Logical Backup) 物理备份是指直接复制存储数据库内容的目录和文件,这种类型的备份适用于出现问题时 ...

  8. 机器学习基础一(TP,TN,FP,FN等)

    TP:预测为正向(P),实际上预测正确(T),即判断为正向的正确率 TN:预测为负向(N),实际上预测正确(T),即判断为负向的正确率 FP:预测为正向(P),实际上预测错误(F),误报率,即把负向判 ...

  9. Linux可以生产uImage

    默认kernel只生产Image和zImage,若想让kernel生产uImage,需要用到mkimage,这个是uboot可以提供的,位于uboot/tool/目录下,将其加入到环境变量即可.

  10. anaconda的python版本与本地python版本不同时的问题

    在用anaconda,尤其是win下的时候,本地的python版本可能和虚拟环境中需要的python版本不同,而在虚拟环境中使用pip3安装包的时候,仍会出现版本是本地的python版本的情况,虽然并 ...