LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)
和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似。
注意到。在for (auto p: prerequistites)中特判了输入中可能出现的平行边或自环。
代码:
class Solution
{
public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites)
{
// [0, {1, 2, 3}], means after finishing #0, you "might" be able to take #1, #2, #3
// That is, you must finish #0, before trying to take #1, #2, #3
map<int, vector<int>> course_chain;
vector<int> in_degree(numCourses, 0);
queue<int> q;
vector<int> ret; for (auto p: prerequisites)
{
// self-loop, return empty vector.
if (p.first == p.second)
{
return vector<int>();
}
// no duplicate edges input
if (find(course_chain[p.second].begin(), course_chain[p.second].end(), p.first) == course_chain[p.second].end())
{
course_chain[p.second].push_back(p.first);
++ in_degree[p.first];
}
}
for (size_t i = 0; i < numCourses; ++ i)
{
if (in_degree[i] == 0)
{
q.push(i);
}
}
for (; !q.empty(); q.pop())
{
int pre_course = q.front();
ret.push_back(pre_course);
for (auto it = course_chain[pre_course].begin(); it != course_chain[pre_course].end(); ++ it)
{
-- in_degree[*it];
if (in_degree[*it] == 0)
{
q.push(*it);
}
}
} return ret.size()==numCourses? ret: vector<int>();
}
};
LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)的更多相关文章
- [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 ...
- [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 课程安排II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- 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 ...
- (medium)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 207. Course Schedule(拓扑排序)
题目 There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have p ...
- Leetcode 210 Course Schedule II
here are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prere ...
- 【LeetCode】210. Course Schedule II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拓扑排序,BFS 拓扑排序,DFS 参考资料 日期 ...
- 【刷题-LeetCode】210. Course Schedule II
Course Schedule II There are a total of n courses you have to take, labeled from 0 to n-1. Some cour ...
随机推荐
- mac下idea卡顿问题解决
idea在加载相对来说比较大的系统时,经常性出现卡顿,就是直接卡死,以至于写起代码特别难受. 最后找到的解决方案是修改idea.vmoptions中的内存大小 执行 find / -name idea ...
- 在linux系统中,使用tomcat的shutdown.sh脚本停止应用,但是进程还在的解决办法
基本原理为启动tomcat时记录启动tomcat的进程id(pid),关闭时强制杀死该进程 第一步 :vi 修改tomcat下bin/catalina.sh文件,增加几行脚本,主要是记录tomcat的 ...
- jQuery学习笔记之插件开发(4)
jQuery学习笔记之插件开发(4) github源码地址 插件:了让原有功能的增强. 1.插件的种类(3种):局部.全局.选择器插件 1.1封装对象方法的插件 这种类型的插件是把一些常用或者重复使用 ...
- ie8及其以下版本兼容性问题之文本省略
1. 单行文本省略 单行文本省略适用于文本超出内容显示区,则在末尾显示省略号 1.1 普通文本超出省略 普通文本超出显示省略号,示例: .p{ height: 30px line-height: 30 ...
- 《java数据结构与算法》系列之“简单排序"-冒泡,选择,插入
好几天又没写,因为这几天很闲,平时忙的时候自己再累都不会睡着,但是呢这没事了,照理说应该是不瞌睡了,结果还睡着了. 所以说,人很贱.也验证了一句话,没有目标的人其实最无聊.人一定要有自己的工作,这工作 ...
- html 底部虚线
<div style="width: 100%; font-size: 14px; color: #666; border-bottom: 1px dashed #666;" ...
- javeee 字节Buffered
package Zy; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io. ...
- 安卓 九宫格 GridView 的表格布局
首先,请大家理解一下“迭代显示”这个概念,这个好比布局嵌套,我们在一个大布局里面重复的放入一些布局相同的小布局,那些重复的部分是由图片和文字组成的小控件,图片在上方,文字在下方,之后我们只需要把这些小 ...
- 解决postman https请求无返回数据的问题
1.点击右上角的扳手图标 2.点击settings 3.点击general 4.把 ssl certificate verification这项点击关闭
- UNIX C 信号
1.信号处理 #include <signal.h> typedef void(*sighander_t)(int); sighander_t signal(int signum,sigh ...