题目:

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)的更多相关文章

  1. [LeetCode] 207. Course Schedule 课程表

    题目: 分析: 这是一道典型的拓扑排序问题.那么何为拓扑排序? 拓扑排序: 有三件事情A,B,C要完成,A随时可以完成,但B和C只有A完成之后才可完成,那么拓扑排序可以为A>B>C或A&g ...

  2. [leetcode]207. Course Schedule课程表

    在一个有向图中,每次找到一个没有前驱节点的节点(也就是入度为0的节点),然后把它指向其他节点的边都去掉,重复这个过程(BFS),直到所有节点已被找到,或者没有符合条件的节点(如果图中有环存在). /* ...

  3. 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 ...

  4. [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 ...

  5. LeetCode - 207. Course Schedule

    207. Course Schedule Problem's Link ---------------------------------------------------------------- ...

  6. LN : leetcode 207 Course Schedule

    lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...

  7. [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 ...

  8. (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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. Python环境和PyCharm搭建教程

    1.python下载和安装 1.访问Python 官网:https://www.python.org/ 2.以Windows为例,我们选择一个稳定的版本进行安装,这里需要注意选择和自己操作系统类型一致 ...

  2. CSS之定位Position

    前言 之前在<CSS之浮动>中,我当时是想一起说说定位的,因为我在很多地方看到有把float和position放在一起讲的,说它们的一些属性值可以使元素脱离文档流,但是没想到在准备内容的时 ...

  3. JavaServlet类

    "感谢您阅读本篇博客!如果您觉得本文对您有所帮助或启发,请不吝点赞和分享给更多的朋友.您的支持是我持续创作的动力,也欢迎留言交流,让我们一起探讨技术,共同成长!谢谢!" 介绍Ser ...

  4. 牛客网-SQL专项训练10

    ①SQL语句中与Having子句同时使用的语句是:group by 解析: SQL语法中,having需要与group by联用,起到过滤group by后数据的作用. ②下列说法错误的是?C 解析: ...

  5. [TP5] ThinkPHP 默认模块和单模块的设置方式

    由于默认是采用多模块的支持,所以多个模块的情况下必须在URL地址中标识当前模块, 如果只有一个模块的话,可以进行模块绑定,方法是应用的入口文件中添加如下代码: // 绑定当前访问到index模块 de ...

  6. [PHP] 浅谈 Laravel Scout 的存在意义

    注:Laravel Scout 是官方支持的对框架模型数据进行全文检索功能的扩展包. Laravel 的 Scout 与 Eloquent ORM 进行了深度集成,不用开发者再自己进行代码侵入了. L ...

  7. [Ethereum] 浅谈加密商品市场 OpenSea 与 opensea-js

    OpenSea 是用于交易以太坊加密商品的网上商店,主要的商品是 ERC721.ERC1155 标准的 Token. 它的特色就在于,只需要一个部署好的智能合约,你就能在 OpenSea 提供的界面上 ...

  8. WPF 已知问题 InputEventArgs 的 Timestamp 属性是静态的导致事件之间相互影响

    本文记录一个 WPF 已知的设计问题,当前此问题已经被大佬修复,这个设计问题刚好属于比较边缘的模块,我写了这么多年的代码还没有踩到这个坑一次,也没有听到有谁提到这个坑 远古时候,不知道大佬是故意还是失 ...

  9. Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: "/xxx". at createRouterError 的说明和解决

    错误说明 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: & ...

  10. Educational Codeforces Round 160 (Rated for Div. 2)

    A 直接模拟,注意细节 #include<bits/stdc++.h> #define ll long long using namespace std; ll p[15] = {1}; ...