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 ...
随机推荐
- rabbitmq镜像模式初体验
rabbitmq-01: yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm wget ...
- SQLServer 里的三种条件判断的用法:Where GroupBy Having
HAVING 子句对 GROUP BY 子句设置条件的方式与 WHERE 子句和 SELECT 语句交互的方式类似.WHERE 子句搜索条件在进行分组操作之前应用:而 HAVING 搜索条件在进行分组 ...
- 自动换行 word-break:break-all和word-wrap:break-word
1.word-break:break-all;当内容(比如很长的一个单词)到每行的末端时,它会把单词截断显示一部分,下一行显示后一部分. 2.word-wrap:break-word;当内容(比如很长 ...
- 浏览器缓存机制(HTTP缓存机制)
其机制是根据HTTP报文的缓存标识进行的. 过程:浏览器发起HTTP请求 – 服务器响应该请求.那么浏览器第一次向服务器发起该请求后拿到请求结果,会根据响应报文中HTTP头的缓存标识,决定是否缓存结果 ...
- python--8、socket网络编程
socket socket可以完成C/S架构软件的开发.须知一个完整的计算机系统是由硬件.操作系统.应用软件三者组成,具备了这三个条件,一台计算机就可以工作了.但是要跟别人一起玩,就要上互联网(互联网 ...
- HIVE 命令记录
HIVE 命令记录 设置hive运行的队列 hive> set mapreduce.job.queuename=ven12; 打印列名 hive> set hive.cli.print.h ...
- SQL Server对数据进行修改
SQL Server对数据进行修改,修改数据库中的数据. auto"> <tr style="background:red"> <td>编号 ...
- 《Java编程思想》学习笔记(一)
1——面向对象和JVM基础 1.java中的4种访问制权限: (1).public:最大访问控制权限,对所有的类都可见. (2).protect:同一包可见,不在同一个包的所有子类也可见. (3). ...
- dubbo之多版本
当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用. 可以按照以下的步骤进行版本迁移: 在低压力时间段,先升级一半提供者为新版本 再将所有消费者升级为新版本 然后将剩下的 ...
- HDU_1398_母函数
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...