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.

Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

click to show more hints.

解法:拓扑排序topological sort

代码如下:

public class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int [][]matrix=new int[numCourses][numCourses];
int [] indegree =new int[numCourses];
int len=prerequisites.length;
for(int i=0;i<len;i++){
int ready=prerequisites[i][0];
int pre=prerequisites[i][1];
if (matrix[pre][ready] == 0)//防止重复条件
indegree[ready]++;
matrix[pre][ready]=1;
}
int count=0;
Queue<Integer> queue =new LinkedList();
for(int i=0;i<indegree.length;i++){
if(indegree[i]==0)
queue.offer(i);
}
while(!queue.isEmpty()){
int course=queue.poll();
count++;
for(int i=0;i<numCourses;i++){
if(matrix[course][i]!=0){
if(--indegree[i]==0){
queue.offer(i);
}
}
}
}
return count==numCourses;
}
}

  运行结果:

(medium)LeetCode 207.Course Schedule的更多相关文章

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

  2. LeetCode - 207. Course Schedule

    207. Course Schedule Problem's Link ---------------------------------------------------------------- ...

  3. LN : leetcode 207 Course Schedule

    lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...

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

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

  6. (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 prer ...

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

  8. [LeetCode] 207. Course Schedule 课程表

    题目: 分析: 这是一道典型的拓扑排序问题.那么何为拓扑排序? 拓扑排序: 有三件事情A,B,C要完成,A随时可以完成,但B和C只有A完成之后才可完成,那么拓扑排序可以为A>B>C或A&g ...

  9. [leetcode]207. Course Schedule课程表

    在一个有向图中,每次找到一个没有前驱节点的节点(也就是入度为0的节点),然后把它指向其他节点的边都去掉,重复这个过程(BFS),直到所有节点已被找到,或者没有符合条件的节点(如果图中有环存在). /* ...

随机推荐

  1. MySQL中Group By,distinct使用注意事项

    mysql> select * from test; +----+-------+------+-------+ | id | name | age | class | +----+------ ...

  2. Js数组去重复取唯一值

    function isBigEnough(element) { return element >= 10; } var filtered = [12, 5, 8, 130, 44].filter ...

  3. php 同步因子的并发处理

    在php中,如果处理支付时,会涉及到并发. 具体体现在同步通知支付结果和异步通知结果. 拿支付宝来说,同步通知call_back和异步通知notify是没有固定先后顺序的. 有可能notify先通知到 ...

  4. iptables基础信息介绍

    在linux系统下,网络安全,除了有SElinux,另外就是iptables防火墙了,这个是用的最多也是功能非常强大的一个工具,今天就对其简单的架构上技术进行概要描述.让自己后续能够逻辑清晰的处理云环 ...

  5. [转]Neutron演进

    在OpenStack世界中,网络组件最初叫nova-network,它混迹于计算节点nova的代码库中.nova-network可以单独部 署在一台机器上,为了高性能HA也可以和nova-comput ...

  6. http://www.dayandeng.com/ 诈骗网站

    http://www.dayandeng.com/ 诈骗网站   http://www.dayandeng.com/userfiles/media/2018/awzosv16.html   骗取你的京 ...

  7. Windows2012 cannot access netapp CIFS share

    NAS1> options cifs.smb2.signing.requiredcifs.smb2.signing.required off NAS1> options cifs.smb2 ...

  8. jQuery-webcam(.NET)实现WEB摄像头监控

    jQuery-webcam是一个非常好用的摄像头监控工具,DEMO可官方下载地址http://www.xarg.org/project/jquery-webcam-plugin/ 1.下载解压后,jq ...

  9. secure crt 基本设置

    基本设置1.修改设置 为了SecureCRT用起来更方便,需要做一些设置,需要修改的有如下几处: 1.退 出主机自动关闭窗口 Options => Global ptions => Gen ...

  10. 328. Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...