美文网首页leetcode
leetcode 课程表 python

leetcode 课程表 python

作者: DaydayHoliday | 来源:发表于2019-04-18 18:26 被阅读0次

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

    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
    

    相关文章

      网友评论

        本文标题:leetcode 课程表 python

        本文链接:https://www.haomeiwen.com/subject/cuztgqtx.html