[Leetcode] Course Schedule
There are a total of n courses you have to take, labeled from 0
to n - 1
.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
For example:
2, [[1,0]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
2, [[1,0],[0,1]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
拓扑排序。用一个队列存入度为0的节点,依次出队,将与出队节点相连的节点的入度减1,如果入度减为0,将其放入队列中,直到队列为空。如里最后还有入度不为0的节点的话,说明有环,否则无环。
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> graph(numCourses, vector<int>());
vector<int> inDegree(numCourses, );
for (auto u : prerequisites) {
graph[u[]].push_back(u[]);
++inDegree[u[]];
}
queue<int> que;
for (int i = ; i < numCourses; ++i) {
if (inDegree[i] == ) que.push(i);
}
while (!que.empty()) {
int u = que.front();
que.pop();
for (auto v : graph[u]) {
--inDegree[v];
if (inDegree[v] == ) que.push(v);
}
}
for (int i = ; i < numCourses; ++i) {
if (inDegree[i] != ) return false;
}
return true;
}
};
[Leetcode] Course Schedule的更多相关文章
- [LeetCode] 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] Course Schedule 课程清单
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- LeetCode Course Schedule II
原题链接在这里:https://leetcode.com/problems/course-schedule-ii/ 题目: There are a total of n courses you hav ...
- [LeetCode] Course Schedule III 课程清单之三
There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...
- [LeetCode] Course Schedule I (207) & II (210) 解题思路
207. Course Schedule There are a total of n courses you have to take, labeled from 0 to n - 1. Some ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- 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 ...
- Java for LeetCode 207 Course Schedule【Medium】
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- asp.net mvc 简易通用自定义Pager实现分页
asp.net mvc 自定义Pager实现分页 Intro 一个WEB应用程序中经常会用到数据分页,本文将实现一个简单通用的分页组件,包含一个 PagerModel (用来保存页码信息),一个 Ht ...
- 微信小程序开发总结
一.设计 无需开发者开发的 1.小程序加载动画: 2.页面下拉刷新加载样式: 3.微信控件(拥有完整的操作反馈):如弹出框.通知.模态框... 建议用微信自己的 1.加载.反馈样式(全局.局部) ...
- SharePoint 2013 入门教程之入门手册
当我们搭建完环境,创建应用程序和网站集后,就已经正式开启了我们的SharePoint之旅了,进入网站以后,开始基本的使用.设置,了解SharePoint相关特性,下面,来简单了解下SharePoint ...
- JavaScript单元测试框架JsUnit基本介绍和使用
JavaScript单元测试框架JsUnit基本介绍和使用 XUnit framework XUnit是一套标准化的独立于语言的概念和结构集合,用于编写和运行单元测试(Unit tests). 每一个 ...
- 清理mac本内存
如果你很久没有删除xcode的缓存文件,由于你每天要运行新的xcode文件,那么你的硬盘空间会越来越小,位于/Users/*home名称*/Library/Developer/Xcode/Derive ...
- 【Swift】iOS开发历险记(二)
前言 这个系列主要是一些开发中遇到的坑记录分享,有助于初学者跨过这些坑,攒够 7 条发一篇. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯 ...
- AFNetWorking https请求 SSL认证 自制证书
1.服务器会给一个证书,一般为.pem格式证书 2.将.pem格式的证书转换成.cer格式的证书 打开电脑自带终端 ,进入到桌面 cd Desktop 回车回到桌面Desktop Admin$ 输入 ...
- render :template 和 render :parital
1 .这两个都可以在controller和view中使用,而且好像可以替换,只是用:template,rails不会自动加下划线,用:partial,rails会自动添加下划线.而且规范的做法,:te ...
- MyBatis Generator作为maven插件自动生成增删改查代码及配置文件例子
什么是MyBatis Generator MyBatis Generator (MBG) 是一个Mybatis的代码生成器,可以自动生成一些简单的CRUD(插入,查询,更新,删除)操作代码,model ...
- vim 使用 YouCompleteMe
当然前提是先装好vundle 1 在vimrv中加入Bundle 'Valloric/YouCompleteMe' 2 vim +PluginInstall +qall 3 安装一对底层需要的编译的东 ...