Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7f78c8d

Browse files
Merge pull request youngyangyang04#1638 from casnz1601/patch-25
Update 0797.所有可能的路径.md
2 parents 90411f1 + bd2832e commit 7f78c8d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎problems/0797.所有可能的路径.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,5 +296,29 @@ public:
296296
### Java
297297
298298
### Python
299+
```
300+
class Solution:
301+
def __init__(self):
302+
self.result = []
303+
self.path = [0]
304+
305+
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
306+
if not graph: return []
307+
308+
self.dfs(graph, 0)
309+
return self.result
310+
311+
def dfs(self, graph, root: int):
312+
if root == len(graph) - 1: # 成功找到一条路径时
313+
# ***Python的list是mutable类型***
314+
# ***回溯中必须使用Deep Copy***
315+
self.result.append(self.path[:])
316+
return
317+
318+
for node in graph[root]: # 遍历节点n的所有后序节点
319+
self.path.append(node)
320+
self.dfs(graph, node)
321+
self.path.pop() # 回溯
322+
```
299323
300324
### Go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /