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 ...
随机推荐
- 如何对HTMLTestRunner 进行输出print 进行修改
在 HTMLTestRunner 模块中,在运行代码后,在输入的html页面值出现了特别简单的一个页面,那么现在如何将HTML页面中输出的更多print 在 HTMLTestRunner.py文件中查 ...
- SQL之LEFT JOIN,EIGHT JOIN,INSERT JOIN的区别
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...
- B - Eleven
Problem description Eleven wants to choose a new name for herself. As a bunch of geeks, her friends ...
- “阻塞”与"非阻塞"与"同步"与“异步"
链接:http://www.zhihu.com/question/19732473/answer/20851256来源:知乎 “阻塞”与"非阻塞"与"同步"与“ ...
- Django学习案例一(blog):三. 模型生成数据
1. 什么是模型models Django中以创建类的形式来创建数据表. 在编写代码的过程中,所有对数据库的操作,都是对类和类的对象进行操作. ORM对象关系映射(Object relation ma ...
- Java多线程-synchronized关键字
进程:是一个正在执行中的程序.每一个进程执行都有一个执行顺序.该顺序是一个执行路径,或者叫一个控制单元. 线程:就是进程中的一个独立的控制单元.线程在控制着进程的执行. 一个进程中至少有一个线程 Ja ...
- Ionic3 环境搭建以及基础配置实现(更新中)
GitHub:https://github.com/Teloi 环境配置输入以下命令安装 Ionic (如果刚才设置了淘宝镜像源,可以使用 cnpm 代替 npm):npm install -g io ...
- C#使用各种时间戳及转换
/// <summary> /// DateTime时间格式转换为13位带毫秒的Unix时间戳 /// </summary> /// <param name=" ...
- MSP430之section(1)
1 Intro The smallest unit of an object file is a section. A section is a block of code or data that ...
- 身份认证防止重放攻击的challenge-response方法
或者叫询问-应答机制. 基于挑战/应答(Challenge/Response)方式的身份认证系统就是每次认证时认证服务器端都给客户端发送一个不同的"挑战"字串,客户端程序收到这个& ...