(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),直到所有节点已被找到,或者没有符合条件的节点(如果图中有环存在). /* ...
随机推荐
- maven Spring 4.2+SpringMVC+dubbo解决TypeProxyInvocationHandler.invoke(SerializableTypeWrapper.java:239)
java.lang.NullPointerException org.springframework.core.SerializableTypeWrapper$TypeProxyInvocationH ...
- maven学习笔记(基本的命令和概念)
mvn创建新项目: mvn archetype:generate -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -Dpackage ...
- UI设计的重要性--避免二义性的输入提示
昨天晚上发现了西安公路客运网上售票系统网站的密码找回系统存在安全漏洞,得出的结论是:密码找回页的漏洞: 1.用户名栏支持用户名.身份证.电话三种任意一种匹配.2.这一步是关键,密码找回问题提示栏居 ...
- POJ #1015 - Jury Compromise - TODO: POJ website issue
(poj.org issue. Not submitted yet) This is a 2D DP problem, very classic too. Since I'm just learnin ...
- IntelliJ IDEA设置自动导入包
IntelliJ IDEA可以自动优化导入包,但是有多个同名的类位于不同的包时,需要自己手动使用Alt + Enter进行导入. Settings→Editor→General→Auto Import ...
- 查看CentOS版本方法
查看内核版本 这个命令适用于所有的linux,包括Redhat.SuSE.Debian.Centos等发行版. root@MyMail ~ # uname Linux root@MyMail ~ # ...
- linux下查看进程运行的时间
原文链接:http://www.centoscn.com/CentOS/2014/0403/2724.html 可通过ps 来查看,通过参数 -o 来查看 例: ps -eo pid,tty,user ...
- 怎样减少FLASH影片文件过大——绝对好用
网站建设中怎样减少FLASH影片文件过大 一,制作前的处理 1声音(mp3): GoldWave中打开需要处理的mp3,然后把它另存为---在最下一栏的属性中选择较低的字节数,例如,本来的mp3 ...
- [摘]PE中安装Windows 7/8
1. 用WinPE U盘启动盘进入电脑.2. 格式化C盘,C盘一定要是激活状态(活动状态).3. 把Win8的ISO文件提取到D:/win8下 4. 从提取的文件中拷备:bootmgr boot(整个 ...
- 287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...