【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 ...
随机推荐
- Failed building wheel for netifaces
目录 文章目录 目录 问题 解决 问题 安装 OpenStackClient 的时候发现问题: Failed building wheel for netifaces Running setup.py ...
- fiddler之请求过滤(Filters)
开启fiddler后,会监听所有的请求,在大多数情况下,我们只需要监听部分请求,此时可以使用Filters功能去控制. 界面显示如下: 默认情况下过滤是未开启的,需要勾选“user filters”进 ...
- UnityEventSystem
能够处理各种UI事件: IPointerEnterHandler:当指针进入 void OnPointerEnter(PointerEventData eventData); IPointerExit ...
- Spring的应用上下文ApplicationContext
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes() ...
- jmeter遍历时间戳
list如下 实现步骤 实现步骤其实很简单,只需要一个foreach控制器,和一段转换时间戳的代码 第一步把时间戳提取出来 第二步把提取的时间戳传入foreach控制器,然后在控制器下面遍历转换 im ...
- Centos快速安装 Memcached
rpm qa|grep memcached //首先检查memcache是否已经安装完成 yum install memcached //(提示你是否确认安装输入y)检查完成后执行安装命令 yum i ...
- Go语言入门篇-命令 与 语法
一.命令基础 1. go run : 用于运行命令源码文件(如:go run helloworld.go) 只能接受一个命令源码文件以及若干个库源码文件作为文件参数 其内部操作步骤: (1)先编译源码 ...
- .net core 学习小结之 PostMan报415
首先415的官方解释是:对于当前请求的方法和所请求的资源,请求中提交的实体并不是服务器中所支持的格式,因此请求被拒绝. 也就是说我所准备的数据格式并不是后台代码使用的数据格式 后台代码如下 using ...
- Docker开启ssh服务
一.准备 apt-get update 更新环境 apt-get install vim 安装vim vim /etc/apt/source.list 更换软件源, 我 ...
- java 接入微信 spring boot 接入微信
1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...