-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq0797.py
More file actions
28 lines (25 loc) · 906 Bytes
/
q0797.py
File metadata and controls
28 lines (25 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/python3
from typing import List
class Solution:
def findPath(self, flags: List[bool], graph: List[List[int]], current: int, target: int, path: List[int],
result: List[List[int]]):
if current == target:
path.append(target)
result.append(path.copy())
path.pop()
return
if flags[current]:
return
flags[current] = True
path.append(current)
for index in graph[current]:
self.findPath(flags, graph, index, target, path, result)
path.pop()
flags[current] = False
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
result = list()
length = len(graph)
flags = [False for _ in range(length)]
path = list()
self.findPath(flags, graph, 0, length - 1, path, result)
return result