207. Course Schedule
207. Course Schedule
https://blog.csdn.net/wongleetion/article/details/79433101
问题的实质就是判断一个有向图是否有环,利用入度去解决这个问题
使用bfs解决问题。
初始化时,利用二维vector存储节点间的关系,并存储每个节点的入度,同时将入度为0的节点放入队列,这是图的起始点。
每次将队列中的节点弹出后,然后将对应的课程的入度减少,如果有此时产生入度为0的节点,再加入队列中,直到队列为空。
最终判断整个入度的存储是否还有节点入度不为0。
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<vector<int>> graph(numCourses,vector<int>());
vector<int> inDegree(numCourses,);
for(auto i : prerequisites){
graph[i.second].push_back(i.first);
inDegree[i.first]++;
}
queue<int> q;
for(int i = ;i < numCourses;i++){
if(inDegree[i] == )
q.push(i);
}
while(!q.empty()){
int num = q.front();
q.pop();
for(auto i : graph[num]){
inDegree[i]--;
if(inDegree[i] == )
q.push(i);
}
}
for(int i = ;i < numCourses;i++){
if(inDegree[i] != )
return false;
}
return true;
}
};
210. Course Schedule II
这个题是要你把学习课程的路径求出来,如果有环,就返回空数组。
这个题基本上与Course Schedule代码差不多,只需要每次在queue弹出的时候存入结果就好,还需要判断是否有环。
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> graph(numCourses);
vector<int> indegree(numCourses);
vector<int> result;
for(int i = ;i < prerequisites.size();i++){
graph[prerequisites[i][]].push_back(prerequisites[i][]);
indegree[prerequisites[i][]]++;
}
queue<int> q;
for(int i = ;i < numCourses;i++){
if(indegree[i] == )
q.push(i);
}
while(!q.empty()){
int course = q.front();
q.pop();
result.push_back(course);
for(int i = ;i < graph[course].size();i++){
indegree[graph[course][i]]--;
if(indegree[graph[course][i]] == )
q.push(graph[course][i]);
}
}
vector<int> res;
for(int i = ;i < numCourses;i++){
if(indegree[i] != )
return res;
}
return result;
}
};
310. Minimum Height Trees
https://www.cnblogs.com/grandyang/p/5000291.html
给定一个拥有树性质的无向图,图的每一个节点都可以视为一棵树的根节点。在所有可能的树中,找出高度最小的树,并返回他们的树根。
如果剩余的节点数超过两个,就可以继续去除叶节点,比如剩余了3个节点,那么其实可以把外面的两个节点去掉,就只剩一个节点了,而且以这个节点做根,得到的树的高度最小;而如果剩余了2个节点,那么不论让谁当根,得到的高度都是一样的;如果剩余一个,自然不必多说就是根节点了。
我们开始将所有只有一个连接边的节点(叶节点)都存入到一个队列queue中,然后我们遍历每一个叶节点,通过图来找到和其相连的节点,并且在其相连节点的集合中将该叶节点删去,如果删完后此节点也也变成一个叶节点了,加入队列中,再下一轮删除。那么我们删到什么时候呢,当节点数小于等于2时候停止,此时剩下的一个或两个节点就是我们要求的最小高度树的根节点
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
if(n == )
return {};
vector<int> res;
vector<unordered_set<int>> adj(n);
queue<int> q;
for(int i = ;i < edges.size();i++){
adj[edges[i][]].insert(edges[i][]);
adj[edges[i][]].insert(edges[i][]);
}
for(int i = ;i < n;i++){
if(adj[i].size() == )
q.push(i);
}
while(n > ){
int size = q.size();
n -= size;
for(int i = ;i < size;i++){
int tmp = q.front();
q.pop();
for(auto j : adj[tmp]){
adj[j].erase(tmp);
if(adj[j].size() == )
q.push(j);
}
}
}
while(!q.empty()){
int tmp = q.front();
q.pop();
res.push_back(tmp);
}
return res;
}
};
207. Course Schedule的更多相关文章
- 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 ...
- 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 (2 solutions)
Course Schedule There are a total of n courses you have to take, labeled from 0 to n - 1. Some cours ...
- [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
Course Schedule There are a total of numCourses courses you have to take, labeled from 0 to numCours ...
- (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 ...
随机推荐
- 自定义滚动条mCustomScrollbar
mCustomScrollbar 是个基于 jQuery UI 的自定义滚动条插件,它可以让你灵活的通过 CSS 定义网页的滚动条,并且垂直和水平两个方向的滚动条都可以定义,它通过 Brandon A ...
- java根据年月显示每周
一.页面效果 1.展示7月份的所有周. 2.当前时间2018.08.02 , 显示到本周. 二.前端代码 1.显示层的代码 <span id="weekyear"> ...
- 【Spring】25、Spring代理。 BeanNameAutoProxyCreator 与 ProxyFactoryBean
一般我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置文件的 ...
- Python3 系列之 环境配置篇
以下所有操作是基于 Windows10 和 Python3.6 来进行的,其它平台和 python 版本请自行百度. 高效使用 Visual Studio Code 系列 环境安装 1.Python ...
- 每个JavaScript工程师都应懂的33个概念
摘要: 基础很重要啊! 原文:33 concepts every JavaScript developer should know 译文:每个 JavaScript 工程师都应懂的33个概念 作者:s ...
- Go开发之路 -- Go语言基本语法 - 作业
1. 判断101 - 200之间有多少个素数,并输出所有素数. package main import ( "fmt" ) var count = 0 func prime(a, ...
- ES 5 中 判断数组的方法
源代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- SQL Anywhere5.5: Metadata
http://dcx.sybase.com/1101/en/dbprogramming_en11/ianywhere-data-sqlanywhere-saconnection-getschem633 ...
- 正则与python的re模块
一.正则表达式的语法 正则表达式使用反斜杠字符('\')来表示特殊的形式或者来允许使用特殊的字符而不要启用它们特殊的含义.这与字符串字面值中相同目的的相同字符的用法冲突:例如,要匹配一个反斜线字面值, ...
- 1474 十进制转m进制
1474 十进制转m进制 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 将十进制数n转换成m进制数 m ...