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. 一步一步理解Paxos算法

    一步一步理解Paxos算法 背景 Paxos 算法是Lamport于1990年提出的一种基于消息传递的一致性算法.由于算法难以理解起初并没有引起人们的重视,使Lamport在八年后重新发表到 TOCS ...

  2. 使用Axure制作App原型应该怎样设置尺寸?

    使用Axure制作的原型,如果你没有设置自适应视图的话它是不会自动适应任何设备的. 若要解释清楚这个问题需要的篇幅比较长,请大家自行参考 Point/Pixel/PPI/DPI 的意思和它们之间的关系 ...

  3. zookeeper Keepalived

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提供的功 ...

  4. sqlserver快照,启用基于行版本控制的隔离级别

    在sqlserver标准的已提交读(read committed)隔离级别下,读写操作相互阻塞.未提交读(read uncommitted)虽然不会有这种阻塞,但是读操作可能会读到脏数据,这是大部分用 ...

  5. Visual Studio个人常用快捷键

    Ctrl+F5:运行程序 F9:设置/取消断点 F5:启动调试 F10:逐过程单步调试 F11:逐语句单步调试 按住Ctrl先按K再按D:格式化全部代码 按住Ctrl先按K再按F:将选中代码块格式化 ...

  6. Spring3.0.6定时任务task:scheduled

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. 【extjs】 Extjs中的Ext.grid.Panel隐藏列会显示在表头中解决方法

    在Extjs中的GridPanel会有这样的情况,隐藏列会显示在menuDisabled中,但是这个一般没有什么用处,只是用于后台取值的作用,感兴趣的朋友可以了解下啊,希望本文对你有所帮助   在Ex ...

  8. smarty缓存技术

    后台: <?php //要求:当存在缓存文件,直接输出,不存在缓存文件,自己创建缓存,输出 //步骤: //定义该页面存放缓存文件的路径 $filename="../../cache/ ...

  9. 多线程编程之Windows同步方式

    在Windows环境下针对多线程同步与互斥操作的支持,主要包括四种方式:临界区(CriticalSection).互斥对象(Mutex).信号量(Semaphore).事件对象(Event).下面分别 ...

  10. jQuery图片无缝滚动JS代码ul/li结构

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...