207. Course Schedule
2016-03-22 10:20
253 查看
There are a total of n courses you have to take, labeled from
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
For example:
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
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.
Subscribe to see which companies asked this question
总之从前度为0的开始移除就对了
0to
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.
Subscribe to see which companies asked this question
总之从前度为0的开始移除就对了
public class Solution { public boolean canFinish(int numCourses, int[][] prerequisites) { // init the adjacency list List<Set> posts = new ArrayList<Set>(); for (int i = 0; i < numCourses; i++) { posts.add(new HashSet<Integer>()); } // fill the adjacency list for (int i = 0; i < prerequisites.length; i++) { posts.get(prerequisites[i][0]).add(prerequisites[i][1]); } // count the pre-courses int[] preNums = new int[numCourses]; for (int i = 0; i < numCourses; i++) { Set set = posts.get(i); Iterator<Integer> it = set.iterator(); while (it.hasNext()) { preNums[it.next()]++; } } // remove a non-pre course each time for (int i = 0; i < numCourses; i++) { // find a non-pre course int j = 0; for ( ; j < numCourses; j++) { 869b if (preNums[j] == 0) break; } // if not find a non-pre course if (j == numCourses) return false; preNums[j] = -1; // decrease courses that post the course Set set = posts.get(j); Iterator<Integer> it = set.iterator(); while (it.hasNext()) { preNums[it.next()]--; } } return true; } }
相关文章推荐
- Android攻城狮学习笔记—入门篇一
- 51nod--1079 中国剩余定理
- “自释放”在iOS开发中的应用
- 设计模式之零碎的小模式
- 关于Server.MapPath和HostingEnvironment.MapPath
- super() this() super this 与构造函数
- 防止js注入
- C# PLINQ 内存列表查询优化历程
- 监控mysql 脚本
- 【云计算】Docker 镜像如何设置语言环境?bash: warning: setlocale: LC_ALL: cannot change locale (en_US)
- table合并单元格 colspan(跨列)和rowspan(跨行)
- 209. Minimum Size Subarray Sum
- Ubuntu图形界面和文字界面切换
- Android ListView item中有按钮(Button)不能点击或者条目不能点击的问题
- Android dip/dp sp px 单位转换小结 applyDimension
- PHP并发IO编程之路
- valgrind-3.11.0 交叉编译
- 82. Remove Duplicates from Sorted List II
- OkHttp + Gson
- linux下如何模拟按键输入和模拟鼠标