题目如下:

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.

解题思路:题目明确了没有回路,而且nodes的范围是[2,15],这实在是大大降低了难度,一个深度遍历就能搞定,注意由于结果需要输出path,所以需要记录每次遍历的过程。

代码如下:

class Solution(object):
def copy(self,src,dest):
for i in src:
dest.append(i)
return dest
def allPathsSourceTarget(self,graph):
stack = []
stack.append([0])
step = []
res = []
while len(stack) > 0:
path = stack.pop(0)
if path[-1] == len(graph) - 1:
res.append(self.copy(path,[]))
for i in graph[path[-1]]:
tl = self.copy(path,[])
tl.append(i)
stack.insert(0,tl)
return res

【leetcode】All Paths From Source to Target的更多相关文章

  1. 【leetcode】Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  2. 【leetcode】Unique Paths II

    Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...

  3. 【题解】【排列组合】【素数】【Leetcode】Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  4. 【题解】【矩阵】【回溯】【Leetcode】Unique Paths II

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  5. LeetCode 797. All Paths From Source to Target

    题目链接:https://leetcode.com/problems/all-paths-from-source-to-target/description/ Given a directed, ac ...

  6. 【leetcode】1210. Minimum Moves to Reach Target with Rotations

    题目如下: In an n*n grid, there is a snake that spans 2 cells and starts moving from the top left corner ...

  7. 【LeetCode】未分类(tag里面没有)(共题)

    [334]Increasing Triplet Subsequence (2019年2月14日,google tag)(greedy) 给了一个数组 nums,判断是否有三个数字组成子序列,使得子序列 ...

  8. 【leetcode】688. Knight Probability in Chessboard

    题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...

  9. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

随机推荐

  1. 中国MOOC_面向对象程序设计——Java语言_期末考试编程题_1细胞自动机

    期末考试编程题 返回   这是期末考试的编程题 温馨提示: 1.本次考试属于Online Judge题目,提交后由系统即时判分. 2.学生可以在考试截止时间之前提交答案,系统将取其中的最高分作为最终成 ...

  2. opencv.js小项目demo

    1.博客连接 https://blog.csdn.net/weixin_38361925/article/details/82528529 2.demo连接 https://github.com/mt ...

  3. 【Qt开发】V4L2 API详解 Buffer的准备和数据读取

    前面主要介绍的是:V4L2 的一些设置接口,如亮度,饱和度,曝光时间,帧数,增益,白平衡等.今天看看V4L2 得到数据的几个关键ioctl,Buffer的申请和数据的抓取. 1. 初始化 Memory ...

  4. kafka学习(三)

    kafka 消费者-从kafka读取数据   消费者和消费者群里 kafka消费者从属于消费者群组.一个群组里的消费者订阅的是同一主题,每个消费者接受主题一部分分区的消息.如果我们往群组里添加更多的消 ...

  5. 20191127 Spring Boot官方文档学习(4.10)

    4.10.使用SQL数据库 从使用JdbcTemplate直接的JDBC访问到完整的"对象关系映射"技术(例如Hibernate),Spring框架为使用SQL数据库提供了广泛的支 ...

  6. 【css】子元素浮动到了父元素外,父元素没有随子元素自适应高度,如何解决?

    正常情况 如果子元素没有设置浮动(float),父元素的高度会随着子元素高度的改变而改变的. 设置浮动以后 父元素的高度不会随着子元素的高度而变化. 例如:在一个ul中定义若干个li,并设置float ...

  7. 文件类型 | 命令ln | 软链接硬链接

    1.9文件类型 1.9.1常见文件类型 d:目录 -:普通文件 l:链接文件 b:设备 1.9.2文件后缀名 sh:shell脚本 tar.gz:压缩包 my.cnf:配置文件 test.zip:压缩 ...

  8. [DS+Algo] 002 一维表结构

    目录 1. 顺序表 1.1 分类 1.2 实现方式 1.3 扩容问题 1.4 操作 2. 链表 2.1 分类 2.2 链表相关操作 2.3 链表 VS 顺序表 3. 关于代码实现 1. 顺序表 1.1 ...

  9. [转帖]青岛uber偷拍设备

    爱彼迎民宿路由器暗藏摄像头:官方回应已移除房源 https://www.cnbeta.com/articles/tech/844233.htm 罚款 就是搞笑啊.. 不过现在偷拍设备真多... 5月5 ...

  10. pg_receivewal实践

    测试从pg_receivewal的日志中恢复从库为主库: 主从配置async模式,配置pg_receivewal接收日志pg_receivewal -D /dbaas/pg/data/pg_recei ...