LeetCode 797. All Paths From Source to Target
题目链接:https://leetcode.com/problems/all-paths-from-source-to-target/description/
Given a directed, acyclic graph of
N
nodes. Find all possible paths from node0
to nodeN-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的更多相关文章
- 【LeetCode】797. All Paths From Source to Target 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【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 ...
- 【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 ...
- LeetCode 1059. All Paths from Source Lead to Destination
原题链接在这里:https://leetcode.com/problems/all-paths-from-source-lead-to-destination/ 题目: Given the edges ...
- [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 ...
- 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 ...
- [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 ...
- 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). ...
- [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 ...
随机推荐
- Mac 安装Python3 facewap环境
参考网上大神的方法 1 官网下载安装 2 下载指定版本的源码cmake安装 3 Mac上使用homebrew进行安装(强烈推荐,主要是前两种的openssl模块我没有搞定链接什么的一直报错,一个个下载 ...
- 使用sed在匹配行之后增加一行数据
需求:在原来大量的shell文件中,给出了错误信息打印,现在要求打印错误后直接退出脚本. if [ $? -eq 0 ];then echo_green "done!" else ...
- UTC,BJT时间换算-java
题目内容: UTC是世界协调时,BJT是北京时间,UTC时间相当于BJT减去8.现在,你的程序要读入一个整数,表示BJT的时和分.整数的个位和十位表示分,百位和千位表示小时.如果小时小于10,则没有千 ...
- php7编译安装-php-fpm.service
[Unit]Description=php-fpm - Hypertext PreprocessorAfter=network.target remote-fs.target nss-lookup.t ...
- XMind 8 pro update 7激活方法
激活过程 0.下载XMindCracker.(自行百度下载)1.断网,使用修改hosts方法,在最后一行添加0.0.0.0 www.xmind.net2.将XMindCrack.jar拷贝到XMind ...
- python 12
#! /usr/bin/python a = 1 b = [2, 3] def func(): a = 2 print("in func a:", a) b[0] = 1 prin ...
- CCF-Crontab-201712-3
大概是CCf第三题中最麻烦的一个吧 我的思路其实我觉得还可以,模拟...可是超时了233 只有90分 [ 可是我看网上其他人也是模拟算法啊, 速度还是太慢了 120行, 1个半小时 大部分花在了de ...
- tab切换 原生js
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- vue项目localhost可以访问 ip不能访问
解决方法: 如图所示:
- Map的嵌套 练习
Map的嵌套 练习 利用迭代和增强for循环的两种方式实现如下效果 package cn.ccc; import java.util.HashMap;import java.util.Iterat ...