(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 ...
随机推荐
- How to send Email using C#
try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail. ...
- TKinter之窗口美化 窗口大小、图标等
设置窗口大小.设置窗口标题.设置窗口图标 效果图: 代码示例: #!/usr/bin/env python # _*_ coding:utf-8 _*_ from Tkinter import * r ...
- js跳转到页面中指定的hash
location.hash = "#filter_moreClue";
- winrar 5.21去广告
http://www.rarlab.com/ 把下面的数据复制到“记事本”中,用文件名“rarreg.key搜索”命名该文件,保存到winrar安装文件夹即完成注册 RAR registration ...
- System.AccessViolationException,尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
从事件查看器中发现,IIS不定期崩溃并重启的现象.抓取crash dump文件后,发现能够看到异常,但没有堆栈信息(主要是只会看托管代码的堆栈,非托管的不清楚.),问题表现及dump日志的截图如下: ...
- R(四): R开发实例-map分布图
前几章对R语言的运行原理.基本语法.数据类型.环境部署等基础知识作了简单介绍,本节将结合具体案例进行验证测试. 案例场景:从互联网下载全国三甲医院数据,以地图作为背景,展现各医院在地图上的分布图.全国 ...
- [linux]BASH 的基本语法
最简单的例子 -- Hello World! 关于输入.输出和错误输出 BASH 中对变量的规定(与 C 语言的异同) BASH 中的基本流程控制语法 函数的使用 2.1 最简单的例子 -- ...
- mongodb基本语句使用
mongodb学习:##mongodb基础##数据库常用命令##用户相关##修改.添加.删除集合数据##条件操作符##创建表 ------------------------------------- ...
- 重绘TabControl
本文转载自:http://blog.csdn.net/conmajia/article/details/7596718 作者:野比 (conmajia@gmail.com) 时间:May, 2012 ...
- free命令、buffer与cache的区别
freefree 命令相对于top 提供了更简洁的查看系统内存使用情况: # free total used free shared buffers cached Mem: 255988 231704 ...