(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 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, return the ordering of courses you should take to finish all courses.
There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
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 the correct course order is [0,1]
4, [[1,0],[2,0],[3,1],[3,2]]
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]
. Another correct ordering is[0,2,1,3]
.
思想:拓扑排序,入度为0的点入队。
代码如下:
public class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
int[] ret=new int[numCourses];
int []inDegree=new int[numCourses];
int len=prerequisites.length;
//找到入度为0的节点
for(int i=0;i<len;i++){
inDegree[prerequisites[i][0]]++;
}
Queue<Integer>q=new LinkedList<Integer>();
for(int i=0;i<numCourses;i++){
if(inDegree[i]==0)
q.offer(i);
}
int i=-1;
while(!q.isEmpty()){
int e=q.poll();
ret[++i]=e;
for(int j=0;j<len;j++){
if(prerequisites[j][1]==e){
if(--inDegree[prerequisites[j][0]]==0)
q.offer(prerequisites[j][0]);
} }
}
if(i==numCourses-1)
return ret;
else
return new int[0]; } }
运行结果:
(medium)LeetCode 210.Course Schedule II的更多相关文章
- 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 ...
- LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)
和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...
- [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
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 课程安排II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- [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
Course Schedule II There are a total of n courses you have to take, labeled from 0 to n - 1. Some co ...
- 【刷题-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 ...
- [Leetcode Week4]Course Schedule II
Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...
随机推荐
- zookeeper启动入口
最近正在研究zookeeper,一些心得记录一下,如有错误,还请大神指正. zookeeper下载地址:http://zookeeper.apache.org/releases.html,百度一下就能 ...
- asp.net MVC ViewData详解
转自:http://www.cnblogs.com/gaopin/archive/2012/11/13/2767515.html 控制器向视图中传值ViewData详解 1.将一个字符串传值到视图中 ...
- MySQL 使用mysqld_multi部署单机多实例详细过程 (转)
随着硬件层面的发展,linux系统多核已经是普通趋势,而mysql是单进程多线程,所以先天上对多进程的利用不是很高,虽然 5.6版本已经在这方面改进很多,但是也没有达到100%,所以为了充分的利用系统 ...
- HTML之布局position理解
HTML中的布局,一个比较难以理解的概念,就是position了,W3C上的解释,position有如下几种: 1. static,默认值,也就是在样式中不指定position 2. fixed 3. ...
- jQuery validate在没有校验通过的情况下拒绝提交
下面通过一个简单的例子说明,这个问题,可能是很多人遇到的,验证不通过的时候,依然提交了表单. HTML <form class="survey" id="surve ...
- jquery导航动画
经常在网上看到的,鼠标在导航上移动时,导航底部的横条会自动移动到鼠标悬浮的导航项上. 效果如下图: 利用jquery的 animate 函数,很好实现.代码很简单! 代码如下: <!DOCTYP ...
- Func系列3:自定义模块
简介 Func自带的模块已经非常丰富,但在日常系统运维当中,尤其是面对大规模的服务器集群.不同类别的业务平台,次是Func自带的模块或许已经不能满足我们的需求,所以有必要通过自定义模块来填补这块的不足 ...
- QT5中的信号与槽与C++ 11的function的配合
最近将公司的界面开发库从WX转换到了QT,有了信号和槽,原本我们在使用WX的时候,为了使用信号,我们是使用BOOST的signal2库,到了QT有了信号槽,这个就没什么必要了 但是前段时间使用QT发现 ...
- c++中ifstream读文件的问题(关于eof())
今天帮别人找BUG,是一段关于c++读写文件的问题,使用的是ifstream与outstream类,关于ofstream与ifstream的用法,此处不再獒述,见代码: #include<ios ...
- 输出一个对象的所有属性的值,可以不用反射机制,用JSON处理更方便
String r = ""; ObjectMapper mapper = new ObjectMapper(); r = mapper.writeValueAsString(cre ...