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:

, [[,]]

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.

思路:

此题可抽象为有向图中的环检测问题。常规解法为构建邻接矩阵或邻接链表后通过拓扑排序检测有向图中是否存在环路来解决。若存在环路说明这个小朋友悲剧了,即将面临辍学的危机,否则,小朋友就可以任性的选课直到毕业(然而事实上即使能顺利毕业也并没有什么*用)。

在这里使用了两个set容器数组来构建图(考虑到也许会有重复输入等情况,还有就是懒 - -!),随后进行拓扑排序求得结果。

 class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int> >& prerequisites) { const int n = numCourses;
int cnt = ; set<int> sets[n], grap[n];
queue<int> Queue;//队列,拓扑排序必备 //从给定的输入中构建图,sets[i]表示学习课程i后才能继续学习的后置课程的集合
//grap[i]表示学习课程i所必须的前置课程的集合(入度)
vector<pair<int, int> >::iterator it = prerequisites.begin();
for(; it != prerequisites.end(); it++)
{
sets[(*it).second].insert((*it).first);
grap[(*it).first].insert((*it).second);
} //将不需要前置课程的课程(入度为0的课程)压入队列
for(int i = ; i < n; i++)
if(grap[i].size() == )
Queue.push(i); while(!Queue.empty())
{
//取出队首的课程
int cur = Queue.front();
Queue.pop(); //遍历当前课程(cur)的后置课程集合
//在每一个后置课程(*it)的前置课程的集合(grap[*it])中去掉当前课程
//若去掉当前课程后该课程的前置课程集合为空,则将其压入队列
set<int>::iterator it = sets[cur].begin();
for(; it != sets[cur].end(); it++)
{
grap[*it].erase(cur);
if(grap[*it].size() == )
Queue.push(*it);
}
cnt++;//统计共拓扑出了多少课程
} //若拓扑出的课程数等于总课程数,说明无环,return true,反之,return false
return cnt == n ? : ;
}
};

【LeetCode 207】Course Schedule的更多相关文章

  1. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  2. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

  3. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  4. 【LeetCode题解】136_只出现一次的数字

    目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...

  5. 【LeetCode题解】7_反转整数

    目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...

  6. 【LeetCode题解】350_两个数组的交集Ⅱ

    目录 [LeetCode题解]350_两个数组的交集Ⅱ 描述 方法一:映射 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 Python 实现 [Lee ...

  7. 【LeetCode题解】349_两个数组的交集

    目录 [LeetCode题解]349_两个数组的交集 描述 方法一:两个哈希表 Java 实现 类似的 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 ...

  8. 【LeetCode题解】94_二叉树的中序遍历

    目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...

  9. 【LeetCode题解】144_二叉树的前序遍历

    目录 [LeetCode题解]144_二叉树的前序遍历 描述 方法一:递归 Java 代码 Python 代码 方法二:非递归(使用栈) Java 代码 Python 代码 [LeetCode题解]1 ...

随机推荐

  1. [SQL Server系] -- 约束

    什么是约束? 约束(Constraint)是SQL Server中提供的 自动保存数据库完整性 的一种方法,定义了可输入表或表的列中的数据限制条件. SQL Server中共有5中约束 PRIMARY ...

  2. 如何获取多核、多cpu系统中指定cpu的序列号

    如何获取多核.多cpu系统中指定cpu的序列号作者:SkyJacker(转贴请保持完整并注明作者和出处)http://www.cnpack.orgCnPack IV  QQ Group: 130970 ...

  3. 258. Add Digits

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  4. Intellij IDEA 快速创建Spring Web 项目

    相关软件: Intellij Idea14:http://pan.baidu.com/s/1nu16VyD JDK7:http://pan.baidu.com/s/1dEstJ5f Tomcat(ap ...

  5. Java API —— 泛型

    1.泛型概述及使用 JDK1.5以后出现的机制 泛型是一种特殊的类型,它把指定类型的工作推迟到客户端代码声明并实例化类或方法的时候进行.也被称为参数化类型,可以把类型当作参数一样传递过来,在传递过来之 ...

  6. PHP中该怎样防止SQL注入?

    因为用户的输入可能是这样的: ? 1 value'); DROP TABLE table;-- 那么SQL查询将变成如下: ? 1 INSERT INTO `table` (`column`) VAL ...

  7. Android应用开发学习笔记之Intent

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Intent是什么呢?来看Android官网上的定义: An intent is an abstractdescri ...

  8. Visaul Studio2015安装以及c++单元测试使用方法

    Visual Studio 2015安装流程     vs2015是一款十分好用的IDE,接下来就介绍一下安装流程.这里采用在线安装方式,从官网下载使得安装更加安全. 第一步:在百度中搜索Visual ...

  9. AndroidApplication Fundamentals(Android应用基础)

    AndroidApplication Fundamentals(Android应用基础) Android应用采用Java编程语言来编写,AndroidSDK工具编译我们的代码,连同任何数据和资源文件一 ...

  10. 1741. Communication Fiend(dp)

    刷个简单的DP缓缓心情 1A #include <iostream> #include<cstdio> #include<cstring> #include< ...