码迷,mamicode.com
首页 > 其他好文 > 详细

210 Course Schedule II

时间:2015-07-07 14:28:37      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:

拓扑排序, 输出一种可能的排序结果,207 Course Schedule 稍微变化一点即可得到正确答案

from collections import defaultdict
class Solution:
    # @param {integer} numCourses
    # @param {integer[][]} prerequisites
    # @return {boolean}
    def findOrder(self, numCourses, prerequisites):
        inNum = [0 for i in range(numCourses)]
        dic = defaultdict(list)
        for [v, u] in prerequisites:
            dic[u].append(v)
            inNum[v] += 1
        totalNum = 0
        ans = []
        while 0 in inNum:
            u = inNum.index(0)
            ans.append(u)
            if u in dic:
                for v in dic[u]:
                    inNum[v] -= 1
            inNum[u] = None
            totalNum += 1
        if totalNum == numCourses:
            return ans
        return []

 

210 Course Schedule II

标签:

原文地址:http://www.cnblogs.com/dapanshe/p/4626728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!