####Level:

  Medium

####题目描述:

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?

Example 1:

Input: 2, [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.

Example 2:

Input: 2, [[1,0],[0,1]]
Output: false
Explanation: 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.

Note:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.

####思路分析:

  这题是拓扑排序一道应用,首先,我们应该统计每个点的入度,然后根据拓扑排序的规则,先删去入度为0的点,直到最后点的个数为0,则是正确的排课。

####代码:

public class Solution{
public boolean canFinish(int coursenum,int[][]prerequisites){
int []indegree=new int [coursenum];//记录每门课的入度。
for(int []pair:prerequisites){
indegree[pair[0]]++; //统计每门课的入度
}
Queue<Integer>q=new LinkedList<>(); //存放入度为0的课程,准备删除
for(int i=0;i<coursenum;i++){
if(indegree[i]==0)
q.offer(i);
}
while(!q.isEmpty()){
int key=q.poll();//删除一个入度为0的课程
coursenum--;//课程数减1
for(int[] pair:prerequisites){
if(pair[1]==key){//因为删除了节点,那么和这个节点相连的节点入度减一。
indegree[pair[0]]--;
if(indegree[pair[0]]==0)//如果减一后,这个节点的入度为0,则加入删除队列
q.offer(pair[0]);
}
}
}
return coursenum==0; //如果最终都被删除,则满足排课顺序
}
}

48.Course Schedule(课程安排)的更多相关文章

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

  2. 01Mybatis_课程安排

    课程安排: mybatis和springmvc通过订单商品 案例驱动 第一天:基础知识(重点,内容量多) 对原生态jdbc程序(单独使用jdbc开发)问题总结 mybatis框架原理   (掌握) m ...

  3. 中科院 2014年GCT考前辅导课程安排

    : 2014年GCT考前辅导课程安排 发布时间: 2014-07-14 阅读次数:1225                       默认字体                   9pt       ...

  4. SpringMVC由浅入深day02_1课程安排_2包装类型pojo参数绑定_3集合类型绑定

    springmvc第二天 高级知识 复习: springmvc框架: DispatcherServlet前端控制器:接收request,进行response HandlerMapping处理器映射器: ...

  5. 01_Python 基础课程安排

    Python 基础课程安排 目标 明确基础班课程内容 课程清单 序号 内容 目标 01 Linux 基础 让大家对 Ubuntu 的使用从很 陌生 达到 灵活操作 02 Python 基础 涵盖 Py ...

  6. mybatis由浅入深day01_1课程安排_2对原生态jdbc程序中问题总结

    mybatis 第一天 mybatis的基础知识 1 课程安排: mybatis和springmvc通过订单商品 案例驱动 第一天:基础知识(重点,内容量多) 对原生态jdbc程序(单独使用jdbc开 ...

  7. Linux:课程安排、Linux简介、虚拟机安装、课前准备(常用设置和操作)

    一.课程安排 1)Linux 的作用 商业服务器基本上都是 Linux: 开源软件都先支持 Linux: 大数据分析.机器学习首先选 Linux: 整个互联网地基靠Linux撑起来: Linux 系统 ...

  8. cogs——644. 课程安排问题

    644. 课程安排问题 ★   输入文件:curriculum.in   输出文件:curriculum.out   简单对比时间限制:1 s   内存限制:128 MB 问题描述 一个软件专业的学生 ...

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

随机推荐

  1. go中字符串类型string的用法

    示例 // 字符串类型string的用法 package main import ( "fmt" "unsafe" ) func main() { // 字符串 ...

  2. css day2

    外观属性 color:文本颜色 用于定义文本的颜色 1.预定义的颜色值,如red.green.blue等 2.十六进制,如#FF0000.#FF6600.#29D794等.十六进制是最常用的定义颜色的 ...

  3. Linux命令(干货)

    @ vim 编辑快捷键 ctrl + n 是自动补齐 ctrl + p 是往上选择 ctrl + f 是下一屏幕 ctrl + b 是上一屏幕 w:是移动一个单词 b:是向前一个单词 d^:当前行中, ...

  4. 【python实例】要求输出字符串中最少一个最多八个的所有字符串组合(连续)

    """ 题目:字符串str="ABCDEFGHIJK",要求输出最少一个最多八个的所有组合(向后连续字母) 输出如下: A [0::] AB ABC ...

  5. 新装ubantu 18.04(自用)

    1.下载镜像,制作u盘启动,装机,分区(具体的百度) 2.sudo passwd root     给root用户创建密码 3.解压tar.gz文件 sudo tar -zxvf  pycharm.t ...

  6. hdu 3183 rmq+鸽巢原理

    题目大意: 给你一个数字字符串序列,给你要求删掉的数字个数m,删掉m个数使的剩下的数字字符串的之最小.并输出这个数字: 基本思路; 这题解法有很多,贪心,rmq都可以,这里选择rmq,因为很久没有写r ...

  7. java基础复习(三)

    一.运算符 1.算术运算符 1) 加法(+) 加法   正号  字符串拼接 2) 减法(-) 减法 负号 3) 乘法 (*) 乘法 4) 除法(/) 除法 整数(小数)相除的例子 10/3 =3:   ...

  8. find命令进阶用法(一)

    -cmin n: 查找 exactly n 分钟前内容或属性被最后修过的文件 -cnewer file: 查找内容或属性的最后修改时间晚于file文件的文件 -ctime n: 查找 **n*24** ...

  9. python3.x运行的坑:AttributeError: 'str' object has no attribute 'decode'

    1.Python3.x和Python2.X版本有一些区别,我遇到了两个问题如下: a.第一个报:mysqlclient 1.3版本不对: 解决办法:注释掉这行即可: b.第二个报:字符集的问题: 报错 ...

  10. django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11

    搭建Django2.0+Python3+MySQL5时同步数据库时报错:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 o ...