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 ...
随机推荐
- 《c#高级编程》第4章C#4.0中的更改(六)——动态绑定
一.概念 下面是一些代码示例,说明C#动态绑定的上述特点: 1. 延迟确定类型 ```dynamic obj = GetDynamicObject(); // 获取动态对象obj.DoSomethin ...
- 使用ISS服务器方式跑C#程序
使用ISS服务器方式跑C#程序 VS2010,临时接了一个C#系统的小系统,需要本地调试跑一下 但是老是在conn.open提示06413,简单来说就是连接不上数据库 尝试了很多方法,最后还是决定配置 ...
- 力扣603(MySQL)-连续空余座位(简单)
题目: 几个朋友来到电影院的售票处,准备预约连续空余座位. 你能利用表 cinema ,帮他们写一个查询语句,获取所有空余座位,并将它们按照 seat_id 排序后返回吗? 对于如上样例,你的查询语句 ...
- PolarDB 并行查询的前世今生
简介:本文会深入介绍PolarDB MySQL在并行查询这一企业级查询加速特性上做的技术探索.形态演进和相关组件的实现原理,所涉及功能随PolarDB MySQL 8.0.2版本上线. 作者 | ...
- 十年再出发,Dubbo 3.0 Preview 即将在 3 月发布
简介:随着Dubbo和HSF的整合,我们在整个开源的体系中更多地去展现了 HSF 的能力,能够让更多的人通过使用 Dubbo 像阿里巴巴之前使用 HSF 一样更好的构建服务化的系统. 2011 年, ...
- Flink 和 Iceberg 如何解决数据入湖面临的挑战
简介: 4.17 上海站 Meetup 胡争老师分享内容:数据入湖的挑战有哪些,以及如何用 Flink + Iceberg 解决此类问题. 一.数据入湖的核心挑战 数据实时入湖可以分成三个部分,分别是 ...
- C++编程英语词汇
abstract抽象的 abstraction抽象性.抽象件 access访问 access level访问级别 access function访问函数 adapter适配器 address地址 ad ...
- 几个ABAP FREE面试问题
Text. Text. Text. Text. Text. 电话面试,有几个问题没有回答上.有些问题是此前完全不了解的,有些是学过但因为好久不用已经忘记.这里试着重新回答一下. 1,如何创建bapi? ...
- Solution Set - 数论相关
绝了,六道题都差一步想出来或者差一个细节就开始看题解. CF906D Link&Submission. 要求 \(a^b\bmod p\),那就要求 \(b\bmod \varphi(p)\) ...
- 【Oracle】Oracle数据库多实例安装
需求:因为需要从RAC的多实例迁移至单虚拟机的多实例.因此,简要概述一下,如何安装数据库的多实例. 不管是Oracle 11g还是10g的多实例,其基本思路都是一致的. 1.调用dbca 在root账 ...