210. 课程表 II

 1 class Solution
 2 {
 3     vector<int> res;
 4 public:
 5     vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites)
 6     {
 7         vector<vector<int>> graph(numCourses);
 8         vector<int> in_degree(numCourses, 0);
 9         for (int i = 0; i < prerequisites.size(); i++)
10         {
11             in_degree[prerequisites[i][0]]++;
12             graph[prerequisites[i][1]].push_back(prerequisites[i][0]);
13         }
14
15         queue<int> q;
16         vector<bool> vis(numCourses, false);
17
18         for (int i = 0; i < numCourses; i++)
19             if (in_degree[i] == 0)
20                 q.push(i);
21         while (!q.empty())
22         {
23             int sta = q.front();
24             q.pop();
25             vis[sta] = true;
26             res.push_back(sta);
27
28             //有哪些邻边
29             for (int i = 0; i < graph[sta].size(); i++)
30             {
31                 in_degree[graph[sta][i]]--;// 入度-1
32                 if (in_degree[graph[sta][i]] == 0) //入度如果为0,加入队列
33                     q.push(graph[sta][i]);
34             }
35         }
36
37         //0->1->2
38         for (int i = 0; i < numCourses; i++)
39             if (vis[i] == false)//只要有false,则返回{}
40                 return {};//有环
41         return res;
42     }
43 };

原文地址:https://www.cnblogs.com/yuhong1103/p/12633615.html

时间: 2024-10-11 11:52:51

210. 课程表 II的相关文章

LeetCode:课程表II【210】

LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总量以及它们的先决条件,返回你为了学完所有课程所安排的学习顺序. 可能会有多个正确的顺序,你只要返回一种就可以了.如果不可能完成所有课程,返回一个空数组. 示例 1: 输入: 2, [[1,0]] 输出: [0,1] 解释: 总共有 2 门课程.要学习课程 1

leet

# 题名1 两数之和    2 两数相加    3 无重复字符的最长子串    4 寻找两个有序数组的中位数    5 最长回文子串    6 Z 字形变换    7 整数反转    8 字符串转换整数 (atoi)    9 回文数    10 正则表达式匹配    11 盛最多水的容器    12 整数转罗马数字    13 罗马数字转整数    14 最长公共前缀    15 三数之和    16 最接近的三数之和    17 电话号码的字母组合    18 四数之和    19 删除链表

手动转一下田神的2048

手动转一下田神的2048 :http://blog.csdn.net/tc_to_top/article/details/38793869 命令行之2048 2048挺火的游戏,在命令行实现是什么样呢?小尝试了下,这里感谢css大神给debug,还有就是游戏空格处采用特殊字符,因此程序在linux下跑可能会出现乱码,可以手动调整 游戏截图: 代码: 1 #include <iostream> 2 #include <ctime> 3 #include <cstdlib>

memcached哈希表操作主要逻辑笔记

以下注释的源代码都在memcached项目的assoc.c文件中 1 /* how many powers of 2's worth of buckets we use */ 2 unsigned int hashpower = HASHPOWER_DEFAULT; /* 哈希表bucket的级别,(1<<hashpower) == bucket的个数 */ 3 4 /* Main hash table. This is where we look except during expansio

[email&#160;protected] [210]Course Schedule II

Course Schedule II Total Accepted: 13015 Total Submissions: 64067 Difficulty: Medium 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 co

【LeetCode】210. Course Schedule II

Course Schedule II 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

leetcode 210. Course Schedule II

跟leetcode207大致相同,巩固一下把,不在赘述.ac代码 class Solution { public: vector<unordered_set<int>> makegraph(int numCourses, vector<pair<int, int>>& prerequisites){ vector<unordered_set<int>> graph(numCourses); for(auto pre:prere

[leedcode 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 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 l

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