题目描述:

第一次提交:

class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
indegree = [0 for _ in range(numCourses)]
adj = [[] for _ in range(numCourses)]
for cur,pre in prerequisites:
adj[pre].append(cur)
indegree[cur] += 1
res = []
queue = []
for i in range(numCourses):
if indegree[i] == 0:
queue.append(i)
while queue:
i = queue.pop(0)
res.append(i)
for j in adj[i]:
indegree[j] -= 1
if indegree[j] == 0:
queue.append(j)
return res if len(res) == numCourses else []

方法二:dfs + 栈

class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
def dfs(i, adjacency, flags):
if flags[i] == -1: return True
if flags[i] == 1: return False
flags[i] = 1
for j in adjacency[i]:
if not dfs(j, adjacency, flags): return False
flags[i] = -1
res.append(i)
return True
res = []
adjacency = [[] for _ in range(numCourses)]
flags = [0 for _ in range(numCourses)]
for cur, pre in prerequisites:
adjacency[pre].append(cur)
for i in range(numCourses):
if not dfs(i, adjacency, flags): return []
return res[::-1]

leetcode-210-课程表②的更多相关文章

  1. Java实现 LeetCode 210 课程表 II(二)

    210. 课程表 II 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0, ...

  2. [LeetCode] 210. 课程表 II

    题目链接:https://leetcode-cn.com/problems/course-schedule-ii/ 题目描述: 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前 ...

  3. LeetCode:课程表II【210】

    LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...

  4. LeetCode:课程表【207】

    LeetCode:课程表[207] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹 ...

  5. [leetcode]210. Course Schedule II课程表II

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  6. [LeetCode] 210. Course Schedule II 课程清单之二

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  7. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. Leetcode 630.课程表III

    课程表III 这里有 n 门不同的在线课程,他们按从 1 到 n 编号.每一门课程有一定的持续上课时间(课程时间)t 以及关闭时间第 d 天.一门课要持续学习 t 天直到第 d天时要完成,你将会从第 ...

  9. Leetcode 207.课程表

    课程表 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总 ...

  10. LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)

    和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...

随机推荐

  1. 国内pypi镜像

    V2EX pypi.v2ex.com/simple 豆瓣 http://pypi.douban.com/simple 阿里云(推荐使用) http://mirrors.aliyun.com/pypi/ ...

  2. python语言和R语言实现机器学习算法

    <转>机器学习系列(9)_机器学习算法一览(附Python和R代码)   转自http://blog.csdn.net/han_xiaoyang/article/details/51191 ...

  3. P3410 /// 最大流最小割

    题目大意: https://www.luogu.org/problemnew/show/P3410 题解 https://www.cnblogs.com/2020pengxiyue/p/9463055 ...

  4. 在vue中运用mt-loadmore 实现上拉加载,下拉刷新

    元旦了,给手残党直接复制的机会,代码如下: 1. :style="{'-webkit-overflow-scrolling': scrollMode}" 最外层div设置,以便兼容 ...

  5. 随便写点什么,证明我还活着,VS2010出现的问题

    今天使用VS2010的过过程中出现了这个问题,哎... 简单地说,其实就是在我的sln里面有某个proj出问题了, 问题是某个或者某几个依赖项出问题了, 怎么办呢,没办法,只能一个一个排查, 我把所有 ...

  6. linux crontab 计划任务编写

    在linux中启动crontab服务: /etc/init.d/crond start crontab的命令格式 crontab -l 显示当前的crontab 文件(默认编写的crontab文件会保 ...

  7. 解决 AUTH` failed: ERR Client sent AUTH, but no password is set [tcp://127.0.0.1:6379]

    页面报错: ConnectionException In AbstractConnection.php line 155 AUTH` failed: ERR Client sent AUTH, but ...

  8. leetcood学习笔记-67-二进制求和

    题目描述: 第一次提交: class Solution: def addBinary(self, a: str, b: str) -> str: list_a,list_b=[],[] for ...

  9. QT 环境变量配置

    //注意每个人的习惯不一样 在系统变量中新建: { QT = C:\Qt\Qt5.13.1\5.13.1 QT_TOOL = C:\Qt\Qt5.13.1\Tools } 然后在path 中加入 { ...

  10. 几道noip2018提高组初赛的题

    以下做法来均自llj @Nicodafagood 一.单项选择题 7. 在一条长度为 1 的线段上随机取两个点,则以这两个点为端点的线段的期望 长度是( ).A. 1 / 2B. 1 / 3C. 2 ...