给一个有 n
个结点的有向无环图,找到所有从 0
到 n-1
的路径并输出(不要求按顺序)
二维数组的第 i 个数组中的单元都表示有向图中 i 号结点所能到达的下一些结点(译者注:有向图是有方向的,即规定了a→b你就不能从b→a)空就是没有下一个结点了。
示例: 输入: [[1,2], [3], [3], []] 输出: [[0,1,3],[0,2,3]] 解释: 图是这样的: 0--->1 | | v v 2--->3 这有两条路: 0 -> 1 -> 3 和 0 -> 2 -> 3.
思路:
DFS+回溯。
class Solution(object):
def allPathsSourceTarget(self, graph):
"""
:type graph: List[List[int]]
:rtype: List[List[int]]
"""
res = list()
n = len(graph)
def dfs(start, tmp):
if graph[start] == [] and start == n - 1:#没有下一个节点
tmp += graph[start]
res.append(tmp[:])
return
l = graph[start]
for node in l:
tmp.append(node)
dfs(node, tmp)
tmp.pop()
dfs(0, [0])
return res