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

  1. LeetCode - 207. Course Schedule

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

  2. LN : leetcode 207 Course Schedule

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

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

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

  5. 【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 ...

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

  7. 【刷题-LeetCode】207. Course Schedule

    Course Schedule There are a total of numCourses courses you have to take, labeled from 0 to numCours ...

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

随机推荐

  1. mysql查看执行sql语句的记录日志

    开启日志模式 -- 1.设置 -- SET GLOBAL log_output = 'TABLE';SET GLOBAL general_log = 'ON';  //日志开启 -- SET GLOB ...

  2. Java 多态 ——一个案例 彻底搞懂它

    最近,发现基础真的hin重要.比如,Java中多态的特性,在学习中就是很难懂,比较抽象的概念.学的时候就犯糊涂,但日后会发现,基础在日常工作的理解中占有重要的角色. 下面,我将用一个代码实例,回忆和巩 ...

  3. linux部分常见指令

    游走指令 cd: 进入指定位置 cd /   进入到根目录   cd /home   进入到home文件夹 cd - 进入上次所在文件夹    比如  在 / 时 cd /usr/local到loca ...

  4. 【pygame游戏编程】第一篇-----创建一个窗口

    下面我们一起来创建一个背景为蓝色的窗口作为游戏编程的开始: import sys import pygame def creat_screen(): #初始化pygame pygame.init() ...

  5. Installing Fonts programatically C#

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. 【代码笔记】Web-ionic-安装及第一个app

    一,下载ionic v1.0.1版本,下载地址为:ionic-v1.0.1.zip. ionic 最新版本下载地址:http://ionicframework.com/docs/overview/#d ...

  7. 学习css(TODO)

    1. css 是一个什么样的角色? 答:css 负责控制网页的样式. 扩展:div + css 是经典的网页布局.实现网页内容与表现相分离. 2. css 的使用方式? 答:1. 内联式:直接在 HT ...

  8. java 约束配置文件和本地约束

    一.寻找spring配置文件约束头(也可直接复制已有的) 1.在本地文件夹解压spring核心包(dist) 例:核心包的约束位置(D:\JavaSources\spring-framework-4. ...

  9. PL/SQL Developer使用小技巧

    1.PL/SQL Developer记住登陆密码         在使用PL/SQL Developer时,为了工作方便希望PL/SQL Developer记住登录Oracle的用户名和密码:    ...

  10. 云卡门禁安卓SDK_BLEDOOR_SDK_ANDROID_2016_12_15

    package com.bosk.bledoor.sdk; //sdk包的开门服务类,AndroidManifest.xml 必须注册 //<service //android:name=&qu ...