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 ...
随机推荐
- 机器学习——Day 2 简单线性回归
写在开头 由于某些原因开始了机器学习,为了更好的理解和深入的思考(记录)所以开始写博客. 学习教程来源于github的Avik-Jain的100-Days-Of-MLCode 英文版:https:// ...
- 「LOJ10150」括号配对
[题目] Hecy 又接了个新任务:BE 处理.BE 中有一类被称为 GBE. 以下是 GBE 的定义: 空表达式是 GBE 如果表达式 A 是 GBE,则 [A] 与 (A) 都是 GBE 如果 A ...
- Spark2.2,IDEA,Maven开发环境搭建附测试
前言: 停滞了一段时间,现在要沉下心来学习点东西,出点货了. 本文没有JavaJDK ScalaSDK和 IDEA的安装过程,网络上会有很多文章介绍这个内容,因此这里就不再赘述. 一.在IDEA上安装 ...
- ROS-导航功能-Gazebo
前言:仿真的整体思路,先启动仿真环境,再启动导航功能. 前提:已下载并编译了相关功能包集,如还未下载,可通过git下载:https://github.com/huchunxu/ros_explorin ...
- IDEA 使用 git (码市)
1.下载 git,并安装(一直下一步) 2.使用IDEA,检出项目,检出方式选择:git, 3.如果项目有修改,上传修改的文件 4.下载 SourceTree(git的图形化工具),并安装(一直下一步 ...
- Ubuntu下搭建repo服务器(一): 配置gitosis
1. 说明 服务器端IP: 192.168.1.126,下文简称:A端: 客户端IP: 192.168.130.19,下文简称:B端: Android工程代号:17435. 2. 安装必要软件(A端) ...
- php数据库批量删除
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- AI.框架理论.语义网.语言间距.孤单
刷个博客,转载自于科学网:AI.框架理论.语义网.语言间距.孤单 一:引言: AI几乎是计算机科学家的梦想,自动化比计算机发展的要早的多.早期的自动化节省了大量人力,激发了人类懒惰的滋长和对自身进化缓 ...
- Spring MVC 中的基于注解的 Controller(转载)
终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法 ...
- 预备篇 I :范畴与函子
拓扑是研究几何图形或空间在连续改变形状后还能保持不变的一些性质的一个学科.它只考虑物体间的位置关系而不考虑它们的形状和大小. 拓扑是集合上的一种结构. 拓扑英文名是Topology,直译是地志学,最早 ...