LeetCode 207. Course Schedule 课程表 (C++/Java)
题目:
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?
Example 1:
Input: 2, [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:
Input: 2, [[1,0],[0,1]]
Output: false
Explanation: 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.
分析:
给定n门课程和他们之间的先修关系,例如[1, 0]就表示修1这门课程要先修完0,判断能否学完所有的课程。
很明显,如果课程中存在循环,就不能完成全部课程。记录每门课程之间的先后顺序,用map存储起来。遍历所有的课程,使用visit数组用来记录每门课程的访问状态,1表示正在访问,2表示已经访问过,访问课程后,在遍历这门课的后续课程,递归执行,如果出现访问的课程状态为1,也就是正在访问,那么表示图中存在环,返回false即可。
程序:
C++
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
courses = vector<vector<int>> (numCourses);
for(auto a:prerequisites){
courses[a[1]].push_back(a[0]);
}
vector<int> visit(numCourses);
for(int i = 0; i < numCourses; ++i)
if(dfs(i, visit))
return false;
return true;
}
private:
bool dfs(int curr, vector<int>& visit){
if(visit[curr] == 1)
return true;
if(visit[curr] == 2)
return false;
visit[curr] = 1;
for(auto i:courses[curr])
if(dfs(i, visit))
return true;
visit[curr] = 2;
return false;
}
vector<vector<int>> courses;
};
Java
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
for(int[] arr:prerequisites){
List<Integer> list = courses.getOrDefault(arr[1], new ArrayList<>());
list.add(arr[0]);
courses.put(arr[1], list);
}
int[] visit = new int[numCourses];
for(int i = 0; i < numCourses; ++i){
if(dfs(i, visit))
return false;
}
return true;
}
private boolean dfs(int curr, int[] visit){
if(visit[curr] == 1)
return true;
if(visit[curr] == 2)
return false;
visit[curr] = 1;
List<Integer> list = courses.get(curr);
if(list != null){
for(int i:list){
if(dfs(i, visit))
return true;
}
}
visit[curr] = 2;
return false;
}
private Map<Integer, List<Integer>> courses = new HashMap<>();
}
LeetCode 207. Course Schedule 课程表 (C++/Java)的更多相关文章
- [LeetCode] 207. Course Schedule 课程表
题目: 分析: 这是一道典型的拓扑排序问题.那么何为拓扑排序? 拓扑排序: 有三件事情A,B,C要完成,A随时可以完成,但B和C只有A完成之后才可完成,那么拓扑排序可以为A>B>C或A&g ...
- [leetcode]207. Course Schedule课程表
在一个有向图中,每次找到一个没有前驱节点的节点(也就是入度为0的节点),然后把它指向其他节点的边都去掉,重复这个过程(BFS),直到所有节点已被找到,或者没有符合条件的节点(如果图中有环存在). /* ...
- 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 课程安排
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 prereq ...
- (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 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 ...
- Java for 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 ...
随机推荐
- ImageJ软件使用教程(三):目标计数
目录 多点工具法 阀值分割法 二值化 填充分割 自动计数 显示结果 总结 参考资料 本文以钢筋计数为例,讲解一下如何使用ImageJ软件进行计数,这里只介绍两种方法: 多点工具法 阀值分割法 钢筋计数 ...
- 大厂面试题:ReentrantLock 与 synchronized异同点对比
写在开头 在过去的博文中我们学习了ReentrantLock 与 synchronized这两种Java并发使用频率最高的同步锁,在很多大厂面试题中有个经典考题: ReentrantLock 与 sy ...
- 力扣520(java)-检测大写字母(简单)
题目: 我们定义,在以下情况时,单词的大写用法是正确的: 1.全部字母都是大写,比如 "USA" .2.单词中所有字母都不是大写,比如 "leetcode" . ...
- 性能提升 57% ,SMC-R 透明加速 TCP 实战解析 | 龙蜥技术
简介:SMC-R 是如何加速 TCP 应用? 编者按:TCP 协议作为当前使用最为广泛的网络协议,场景遍布移动通信.数据中心等.对于数据中心场景,通过弹性 RDMA 实现高性能网络协议 SMC-R, ...
- Elasticsearch生态&技术峰会 | 阿里云Elasticsearch云原生内核
简介: 开源最大的特征就是开放性,云生态则让开源技术更具开放性与创造性,Elastic 与阿里云的合作正是开源与云生态共生共荣的典范.值此合作三周年之际,我们邀请业界资深人士相聚云端,共话云上Elas ...
- [FE] Quasar 性能优化: 减小 vendor.js 尺寸
默认情况下,出于性能和缓存的原因,Quasar 所有来自 node_modules 的东西都会被注入到 vendor 中. 但是,如果希望从这个 vendor.js 中添加或删除某些内容,可以如下这样 ...
- 这款 AI 代码辅助插件真不错,还能帮你发现 bug!
大家好,我是树哥. 随着 ChatGPT 风靡全球之后,编程界也迎来了许多代码辅助工具,有非常出名的 Github Copilot 工具.今天,树哥给大家介绍一款免费的代码辅助插件,它无需代理上网,直 ...
- WPF 已知问题 InputEventArgs 的 Timestamp 属性是静态的导致事件之间相互影响
本文记录一个 WPF 已知的设计问题,当前此问题已经被大佬修复,这个设计问题刚好属于比较边缘的模块,我写了这么多年的代码还没有踩到这个坑一次,也没有听到有谁提到这个坑 远古时候,不知道大佬是故意还是失 ...
- dotnet 构建还原失败 NuGet.targets 错误可能原因
我在一次断电关机之后,发现我所有的项目都构建不通过了,提示在 NuGet.targets 文件的第 130 行错误.原因就是存在有某个被项目引用的 NuGet 包被损坏,在进行 NuGet 还原时读取 ...
- GeoHash实现附近的人功能(如微信附近的人、共享单车附近的车辆、美团附近的商家)
如何查找当前点(118.818747°E,32.074497°N)附近500米的人? 这一类功能很常见(如微信附近的人.共享单车附近的车辆.美团附近的商家),那在java中是如何实 现的呢? 1 实现 ...