题目链接: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. 搭建vue的开发环境

    随手笔记:win7 64bit 1.安装node,直接从node官网下载,安装即可. 2.命令行输入 node -v 查看是否安装成功,显示node的版本号即安装成功.安装成功后,输入node,进入n ...

  2. Linux 常用命令笔记-2

    注意事项: 沟通项目需求:1.项目背景和目的 哪个团队.项目Wiki? 数据库登陆:mysql -uroot -pabc@0326 -h127.0.0.1 -P4004 -A set names ut ...

  3. Java——string类型与date类型之间的转化

    String类型转化为Date类型 方法一 Date date=new Date("2019-01-25"); 方法二 String =(new SimpleDateFormat( ...

  4. 爬虫基础之urllib库(代码演示)

    # 自定义opener   from urllib.request import ProxyHandler,build_opener from urllib.error import URLError ...

  5. C++实验四

    // 类graph的实现 #include "graph.h" #include <iostream> using namespace std; // 带参数的构造函数 ...

  6. 作业一 :关于C语言

    C语言是计算机专业的基础课,同时也是计算机专业的第一个入门语言,学好C语言母庸质疑.就目前来看,在C语言中已经学习的内容有:基本运算符及表达式.输入输出函数.选择 结构程序设计.循环结构程序设计.数组 ...

  7. Eclipse导入已有的项目后项目报错的解决办法

    第一种:jsp报错 选择windows-->preference-->列表找到Validation-->点击Disable All ->> Apply ->> ...

  8. sed常用操作命令

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据进行替换.删除.新增.选取等特定工作. 命令格式: sed [OPTION]... {script-only-i ...

  9. centos安装mycat

    1.参考前文安装jdk 2.官网 http://www.mycat.io/ 或 http://dl.mycat.io/ 下载mycat 3.解压安装 cd /usr/local cp /home/ta ...

  10. Linux安装MySQL_5.6

    E&T: CentOS_7.4 64位; mysql-5.6.42-linux-glibc2.12-x86_64.tar; Xftp5; Xshell5; P1.下载Linux环境下的MySQ ...