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的更多相关文章

  1. 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 ...

  2. LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)

    和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...

  3. [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 ...

  4. 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 ...

  5. [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 ...

  6. [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 ...

  7. 【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 ...

  8. 【刷题-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 ...

  9. [Leetcode Week4]Course Schedule II

    Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...

随机推荐

  1. Redis配制说明

    配置文件参数说明:  1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程     daemonize no 2. 当Redis以守护进程方式运行时,Redis默 ...

  2. JQueryMobile + PhoneGap 经验总结

    1. pageinit & pageshow JQM的官方手册重点提醒了使用$(document).bind(‘pageinit’)代替$(document).ready(). 但当你需要对某 ...

  3. 初级——程序如何打包成apk文件

    将Eclipse Android项目打包成APK文件是本文要介绍的内容,主要是来了解并学习Eclipse Android打包的内容,具体关于Eclipse Android内容的详解来看本文.Eclip ...

  4. 程序员书单_J2EE专题

    Enterprise JavaBeans 3.0中文版(第5版) http://download.csdn.net/detail/shenzhq1980/9104015 J2EE设计模式 http:/ ...

  5. HackerRank "Lucky Numbers"

    Great learning for me:https://www.hackerrank.com/rest/contests/master/challenges/lucky-numbers/hacke ...

  6. Redis介绍及实践分享

    1.Redis是什么 1)Redis是REmote DIctionary Server的缩写,是一个key-value存储系统 2)Redis提供了一些丰富的数据结构,包括Strings,Lists, ...

  7. 【VB技巧】VB ListView 控件功能使用详解

    来源:http://lcx.cc/?i=494 ListView控件 在工具箱上击鼠标右键,选择快捷菜单的Components(部件)项,在控件列表中选择Microsoft Windows Commo ...

  8. GCC 编译使用动态链接库和静态链接库

    1 库的分类 根据链接时期的不同,库又有静态库和动态库之分. 静态库是在链接阶段被链接的(好像是废话,但事实就是这样),所以生成的可执行文件就不受库的影响了,即使库被删除了,程序依然可以成功运行. 有 ...

  9. 解决pdm打开只显示表名不显示字段的步骤

    解决pdm打开只显示表名不显示字段的方法 选中PDM 依次点击 工具-->显示参数选择-->content 下面的table ,右边勾选上columns 点击OK 选择 all symbo ...

  10. spring学习笔记(转)

    [1]搭建环境 1.添加jar包 使用spring需要 sring.jarcommons-loggin.jar 如果使用aop组件需要 aspectjweaver.jaraspectjrt.jar 如 ...