【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 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的更多相关文章
- 【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 ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 【题解】【排列组合】【素数】【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 ...
- 【题解】【矩阵】【回溯】【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 ...
- LeetCode 797. All Paths From Source to Target
题目链接:https://leetcode.com/problems/all-paths-from-source-to-target/description/ Given a directed, ac ...
- 【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 ...
- 【LeetCode】未分类(tag里面没有)(共题)
[334]Increasing Triplet Subsequence (2019年2月14日,google tag)(greedy) 给了一个数组 nums,判断是否有三个数字组成子序列,使得子序列 ...
- 【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 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
随机推荐
- C#正则的使用
c#使用正则表达式要用到System.Text.RegularExprssions命名空间 官方API Regex类是用于匹配表达式: 通常Regex分为静态类和实例化俩种方式.那这俩种有什么区别呢, ...
- RabbitMQ使用(上)
1. 说明 在企业应用系统领域,会面对不同系统之间的通信.集成与整合,尤其当面临异构系统时,这种分布式的调用与通信变得越发重要.其次,系统中一般会有很多对实时性要求不高的但是执行起来比较较耗时的地方, ...
- Lesson 5 The facts
go to extremes走极端 provide... with...向..提供.. go to press付印 suspicious,adj. 可疑的:怀疑的:多疑的 fired---同义词--- ...
- seanborn使用函数regplot回归分析绘图
可以用regplot(x, y, data)绘制回归图.data参数是DataFram类型,x是其中某一列列名,是即将绘制的图的x坐标,y是其中某一列,是图的y坐标 下面代码是对seaborn内置数据 ...
- 应用安全 - CMS - PHPCMS漏洞汇总
CVE-2011-0644 Date: 2011.1 类型: /flash_upload.php SQL注入 影响版本:phpCMS 2008 V2 PHPCMS PHPCMS通杀XSS 在我要报错功 ...
- 【Qt开发】【VS开发】【Linux开发】OpenCV、Qt-MinGw、Qt-msvc、VS2010、VS2015、Ubuntu Linux、ARM Linux中几个特别容易混淆的内容
[Qt开发][VS开发][Linux开发]OpenCV.Qt-MinGw.Qt-msvc.VS2010.VS2015.Ubuntu Linux.ARM Linux中几个特别容易混淆的内容 标签:[Qt ...
- 20191128 Spring Boot官方文档学习(9.11-9.17)
9.11.消息传递 Spring Boot提供了许多包含消息传递的启动器.本部分回答了将消息与Spring Boot一起使用所引起的问题. 9.11.1.禁用事务JMS会话 如果您的JMS代理不支持事 ...
- linux修改用户最大线程数
linux下普通用户最大允许使用线程数为1024: 但是并发量大时,该1024配置项远远不够满足我们的需要,我们可以修改/etc/security/limits.d/90-nproc.conf配置设置 ...
- Spring Boot 2.2.0 正式发布,支持 JDK 13!
Java技术栈 www.javastack.cn 优秀的Java技术公众号 推荐阅读: Spring Boot 2.2.0 正式发布了,可从 repo.spring.io 或是 Maven Centr ...
- phpstudy添加PHP
想在phpstudy2018里面增加一个php版本,操作如下: 一.下载php-7.2.19-ts文件,解压缩,放在相应的目录下: 二.修改Apache的配置文件1.修改httpd.conf 配置,D ...