题目连接

https://leetcode.com/problems/course-schedule-ii/

Course Schedule II

Description

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, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

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 the correct course order is $[0,1]$

4, $[[1,0],[2,0],[3,1],[3,2]]$
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is $[0,1,2,3]$. Another correct ordering is$[0,2,1,3].$

拓扑排序,输出结果
Ps:我喜欢用链式前向星建图。

class Solution {
public:vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {ans.clear();int m = prerequisites.size();inq = new int[numCourses + 10];memset(inq, 0, sizeof(int)* (numCourses + 10));head = new int[numCourses + 10];memset(head, -1, sizeof(int)* (numCourses + 10));G = new edge[m + 10];for (int i = 0; i < m; i++) {int u = prerequisites[i].first, v = prerequisites[i].second;inq[u]++;add_edge(v, u);}queue<int> q;for (int i = 0; i < numCourses; i++) { if (!inq[i])  q.push(i); }while (!q.empty()) {int u = q.front(); q.pop();ans.push_back(u);for (int i = head[u]; ~i; i = G[i].next) {if (--inq[G[i].to] == 0) q.push(G[i].to);}}delete[]G; delete[]inq, delete[]head;return ans.size() == numCourses ? ans : vector<int>();}
private:vector<int> ans;int tot, *inq, *head;struct edge { int to, next; }*G;inline void add_edge(int u, int v) {G[tot].to = v, G[tot].next = head[u], head[u] = tot++;}
};

转载于:https://www.cnblogs.com/GadyPu/p/5020731.html

leetcode Course Schedule II相关推荐

  1. LeetCode Course Schedule II(拓扑排序)

    问题:给出个课程个数,及前提条件对[v,u],即修课程v之前需要修课程u,如果可以输出修的课程顺序 思路: 对于有向图的拓扑排序 一种方式是使用dfs,访问结点的状态分为三种,white,gray,b ...

  2. 207. Course Schedule 210. Course Schedule II

    拓扑排序的问题 套路: 1. 初始化所有的节点的入度为零. 2. 遍历给定的输入要求. 维护一个indegree 的表.记录每个节点的入度. 维护map, 每个节点的后续节点 3. 得到这两个表以后开 ...

  3. 【暴力枚举】LeetCode 90. Subsets II

    LeetCode 90. Subsets II solution1和2均是利用set的,3和4是不利用set的 Solution1:我的答案 迭代法 class Solution { public:v ...

  4. 【DFS】LeetCode 52. N-Queens II

    LeetCode 52. N-Queens II Solution1:我的答案 教科书一样的回溯法 <程序员面试金典>中有一道一毛一样的题啊! class Solution { publi ...

  5. 【数字全排列】LeetCode 47. Permutations II

    LeetCode 47. Permutations II Solution1:我的答案 笨蛋方法:和第46题的思路差不多,把vector换成了set就OK啦~~~ class Solution { p ...

  6. [LeetCode] Word Break II 拆分词句之二

    [LeetCode] Word Break II 拆分词句之二 Given a string s and a dictionary of words dict, add spaces in s to ...

  7. leetcode 210. Course Schedule II | 210. 课程表 II(Java)

    题目 https://leetcode.com/problems/course-schedule-ii/ 题解 本题与 leetcode 207. Course Schedule 基本相同,代码只需要 ...

  8. 从LeetCode 210. Course Schedule II 了解拓扑排序

    问题简述 给定n节课,每节课按0~n-1编号. 在修某些课的时候需要有其它课的基础,必须先上先修课.现在用pair的形式来表示要先修的课,比如 [ [0,1], [1,2] ] 就表示在修课程1之前必 ...

  9. [LeetCode]Single Number II

    今天室友推荐了一道LeetCode上面的练习题Single Number II,题目大概的意思是有一个整形的数组,里面只有一个数是出现一次的,其他的数都出现了三次,然后让把这个数找出来. 第一眼看到这 ...

最新文章

  1. 教你快速撸一个免费HTTPS证书
  2. 使用%的符号问题(只与被除数有关)
  3. Java 编程的动态性,第 7 部分: 用 BCEL 设计字节码--转载
  4. java越权发送邮件_已登录用户 越权 访问action方法怎么解决?
  5. AutoPep8-----Pycharm自动排版工具
  6. 可爱的PNG免扣新年装饰素材,让你海报增加节日气氛
  7. 学习NA,Dynamips实验环境(工大瑞普)下载地址
  8. HackerRank SQL练习题答案大全
  9. 弱电工程施工组织设计
  10. 家庭生涯妙招,必定要看哦
  11. centos系统安装杀毒软件clamav
  12. 计算机ram和rom的工作原理,RAM和ROM的作用与区别详细说明
  13. Circuit Design 贴片晶振的区分
  14. [1151]python连接 redis cluster集群
  15. java快递管理系统
  16. 人脸识别4-百度商用方案调研
  17. 深度学习 warmup 策略
  18. HighwayNet网络详解及复现
  19. ADG配置(主备库环境)
  20. otn 709帧结构

热门文章

  1. IDEA将maven项目复制成一个新的框架/项目
  2. quartz可以指定方法名吗_2020象限仪流星雨肉眼可以看见吗?象限仪座流星雨观测方法...
  3. python经典题库和答案_Python99道经典练习题答案.docx
  4. 工作分析文献综述_学术知识| 如何撰写文献综述
  5. 系统学习机器学习之特征工程(三)--稀疏特征之FM算法
  6. C++查漏补缺之流状态
  7. django python3 异步_详解配置Django的Celery异步之路踩坑
  8. C语言:求矩阵对角线元素的和
  9. delphi xe3 mysql,delphi10.3安装使用mySQL
  10. java Context类