面试被问到了,没做出来,以后不能再有侥幸心理了。
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
网友评论