(medium)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 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.
解法:拓扑排序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的更多相关文章
- 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 ...
- LeetCode - 207. Course Schedule
207. Course Schedule Problem's Link ---------------------------------------------------------------- ...
- LN : leetcode 207 Course Schedule
lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...
- [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 ...
- [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 ...
- (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 ...
- 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 ...
- [LeetCode] 207. Course Schedule 课程表
题目: 分析: 这是一道典型的拓扑排序问题.那么何为拓扑排序? 拓扑排序: 有三件事情A,B,C要完成,A随时可以完成,但B和C只有A完成之后才可完成,那么拓扑排序可以为A>B>C或A&g ...
- [leetcode]207. Course Schedule课程表
在一个有向图中,每次找到一个没有前驱节点的节点(也就是入度为0的节点),然后把它指向其他节点的边都去掉,重复这个过程(BFS),直到所有节点已被找到,或者没有符合条件的节点(如果图中有环存在). /* ...
随机推荐
- windows下android开发环境搭建
JDK的安装和Java环境变量的设置 1 JDK下载地址 JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.h ...
- Ext is not defined
最近由于项目设计到Extjs所以也准备研究一下,可是谁知道刚写好一个demo,运行的时候死活出不来界面,于是用firebug看了一下,出现:Ext is not defined,因为刚开始学也不知道是 ...
- openstack(liberty):部署实验平台(一,基础网络环境搭建)
openstack项目的研究,到今天,算是要进入真实环境了,要部署实验平台了.不再用devstack了.也就是说,要独立controller,compute,storage和network了.要做这个 ...
- [转]java生成随机数字和字母组合
摘自 http://blog.csdn.net/xiayaxin/article/details/5355851 import java.util.Random; public String getC ...
- C#Random()函数详解
随机数的使用很普遍,可用它随机显示图片,用它防止无聊的人在论坛灌水还可以用来加密信息等等.本文讨论如何在一段数字区间内随机生成若干个互不相同的随机数,比如在从1到20间随机生成6个互不相同的整数,并通 ...
- .NET(C#)生成条形码
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- js 字符串类型转为数组类型
以前从来没有想过这个转换,以为直接拼出来就可以了,今天同事问我这个问题,特记录如下. var test='["colkey", "col", "col ...
- (转)C# 使用BackgroundWorker
本文转载自:http://blog.csdn.net/andrew_wx/article/details/6615077 该例子为使用BackgroundWorker在TextBox文本中产生一个10 ...
- jfinal配置rails的数据表
鉴于rails的部署太可怕,所以有了使用rails的建表工具和migration,用jfinal来开发的想法,在此贴一下需要注意的地方 maven配置 <dependency> <g ...
- Linux下高并发socket最大连接数所受的各种限制
http://blog.csdn.net/guowake/article/details/6615728 1.修改用户进程可打开文件数限制 在Linux平台上,无论编写客户端程序还是服务端程序,在进行 ...