leetcode-210-课程表②
题目描述:


第一次提交:
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-课程表②的更多相关文章
- Java实现 LeetCode 210 课程表 II(二)
210. 课程表 II 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0, ...
- [LeetCode] 210. 课程表 II
题目链接:https://leetcode-cn.com/problems/course-schedule-ii/ 题目描述: 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前 ...
- LeetCode:课程表II【210】
LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...
- LeetCode:课程表【207】
LeetCode:课程表[207] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹 ...
- [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 ...
- [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 ...
- 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 ...
- Leetcode 630.课程表III
课程表III 这里有 n 门不同的在线课程,他们按从 1 到 n 编号.每一门课程有一定的持续上课时间(课程时间)t 以及关闭时间第 d 天.一门课要持续学习 t 天直到第 d天时要完成,你将会从第 ...
- Leetcode 207.课程表
课程表 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总 ...
- LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)
和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...
随机推荐
- Hexo使用攻略-添加分类及标签
创建"分类"选项 生成"分类"页并添加tpye属性 打开命令行,进入博客所在文件夹.执行命令 hexo new page categories 成功后会提示: ...
- VS2015 定时服务及控制端
一. 服务端 如下图—新建项目—经典桌面—Windows服务—起名svrr 2. 打到server1 改名为svrExecSqlInsert 右击对应的设计界面,添加安装服务目录结构如图 3. sv ...
- CSS中background的用法
CSS中 background 是一个很基本的而且比较常用的样式 background : background-color || background-image || background-re ...
- python--常用模块:collections 、time、random
一.collections 模块 1:nametuple 生成可以用名字访问内容的元祖 from collections import namedtuple point=namedtuple('p ...
- debian 下设置Ctrl+Alt+T快捷键打开终端
在设置->键盘->快捷键->自定义快捷键->添加 名称:Terminal 命令:gnome-terminal 再右上边点击后 按Ctrl +Alt +T
- Ansible角色
Ansible角色介绍 官方地址: https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html 角色目录 ...
- Eureka备忘
一. 二. 2.2. eureka集群同步集群相关重要的类com.netflix.eureka.registry.PeerAwareInstanceRegistryImpl: 为了保证集群里所有Eur ...
- is与==的区别
is:比较两边的内存地址是否一样 ==:比较两边的数据值是否一样 list1 = [1, 2] list2 = [1, 2] if list1 == list2: # == 是比较数据 print(' ...
- Exception一自定义异常
异常体系的根类是:Throwable Throwable: Error: 重大的问题,我们处理不了.也不需要编写代码处理.比如说内存溢出. Exception: 一般性的错误,是需要我们对编写 ...
- Tomcat 调优的技巧
转载:www.cnblogs.com/wangsen, https://mp.weixin.qq.com/s/WrIsOOyR7o4SwSXMT0Zecg 最近,看到一篇讲述 Tomcat 调优的文章 ...