python课程表代码_leetcode 课程表 python
发布日期:2021-10-26 17:40:27 浏览次数:12 分类:技术文章

本文共 726 字,大约阅读时间需要 2 分钟。

面试被问到了,没做出来,以后不能再有侥幸心理了。

class Solution(object):

def canFinish(self, numCourses, prerequisites):

dict_graph={}

for i in range(numCourses):

dict_graph[i]={}

for prereq in prerequisites:

dict_graph[prereq[0]][prereq[1]]=1

def del0indegree(dict_graph):

tobedel=[]

for node in dict_graph:

if len(dict_graph[node])==0:

tobedel.append(node)

for tobedel_i in tobedel:

del dict_graph[tobedel_i]

for node in dict_graph:

for tobedel_i in tobedel:

if tobedel_i in dict_graph[node]:

del dict_graph[node][tobedel_i]

def has0indegree(dict_graph):

for node in dict_graph:

if len(dict_graph[node].keys())==0:

return True

return False

while has0indegree(dict_graph):

del0indegree(dict_graph)

return True if len(dict_graph.keys())==0 else False

转载地址:https://blog.csdn.net/weixin_33952775/article/details/113678539 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:工控行业学什么编程语言比较好_机器人学中最流行的10种编程语言
下一篇:mysql 备份不了了_mysql不能备份问题

发表评论

最新留言

很好
[***.229.124.182]2024年04月01日 00时42分17秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章