Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
all-paths-from-source-lead-to-destination.py 1.08 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-06-02 23:09 +08:00 . Create all-paths-from-source-lead-to-destination.py
# Time: O(n + e)
# Space: O(n + e)
import collections
class Solution(object):
def leadsToDestination(self, n, edges, source, destination):
"""
:type n: int
:type edges: List[List[int]]
:type source: int
:type destination: int
:rtype: bool
"""
UNVISITED, VISITING, DONE = range(3)
def dfs(children, node, destination, status):
if status[node] == DONE:
return True
if status[node] == VISITING:
return False
status[node] = VISITING
if node not in children and node != destination:
return False
if node in children:
for child in children[node]:
if not dfs(children, child, destination, status):
return False
status[node] = DONE
return True
children = collections.defaultdict(list)
for parent, child in edges:
children[parent].append(child)
return dfs(children, source, destination, [0]*n)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助