11/11 <Topological Sort> 207
207. Course Schedule
我们定义二维数组 graph 来表示这个有向图,一维数组 in 来表示每个顶点的入度。我们开始先根据输入来建立这个有向图,并将入度数组也初始化好。然后我们定义一个 queue 变量,将所有入度为0的点放入队列中,然后开始遍历队列,从 graph 里遍历其连接的点,每到达一个新节点,将其入度减一,如果此时该点入度为0,则放入队列末尾。直到遍历完队列中所有的值,若此时还有节点的入度不为0,则说明环存在,返回 false,反之则返回 true
inDegree[]: 修该门课程的入度,即先修课数量;
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int[] inDegree = new int[numCourses];
Map<Integer, List<Integer>> graph = new HashMap<>();
//Graph: key->课程 value->所需先修课列表
//inDegree[] 下标对应课程,value为对应的入度
for(int i = 0; i < prerequisites.length; i++){
inDegree[prerequisites[i][0]]++;
if(graph.containsKey(prerequisites[i][1])){
graph.get(prerequisites[i][1]).add(prerequisites[i][0]);
}else{
ArrayList<Integer> list = new ArrayList<>();
list.add(prerequisites[i][0]);
graph.put(prerequisites[i][1], list);
}
}
Queue<Integer> queue = new LinkedList<>();
for(int i = 0; i < numCourses; i++){
if(inDegree[i] == 0) queue.add(i);
}
while(!queue.isEmpty()){
int cur = queue.poll();
List<Integer> toTake = graph.get(cur);
for(int i = 0; toTake != null && i < toTake.size(); i++){
inDegree[toTake.get(i)]--;
if(inDegree[toTake.get(i)] == 0) queue.add(toTake.get(i));
}
}
for(int i = 0; i <numCourses; i++){
if(inDegree[i] != 0) return false;
}
return true;
}
}
210. Course Schedule II
用Order[]来存储路线
class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
if(numCourses == 0) return null;
int inDegree[] = new int[numCourses], order[] = new int[numCourses], index = 0;
for(int i = 0; i < prerequisites.length; i++){
inDegree[prerequisites[i][0]]++;
}
Queue<Integer> queue = new LinkedList<Integer>();
for(int i = 0; i < numCourses; i++){
if(inDegree[i] == 0){
order[index++] = i;
queue.offer(i);
}
}
while(!queue.isEmpty()){
int cur = queue.poll();
for(int i = 0; i < prerequisites.length; i++){
if(prerequisites[i][1] == cur){
inDegree[prerequisites[i][0]]--;
if(inDegree[prerequisites[i][0]] == 0){
//If inDegree is zero, then add the course to the order
order[index++] = prerequisites[i][0];
queue.offer(prerequisites[i][0]);
}
}
}
}
return (index == numCourses) ? order : new int[0];
}
}
11/11 <Topological Sort> 207的更多相关文章
- NOIp 11.11/12
最后一场比较正式的NOIp模拟赛,写一发小总结.题目没什么好说的,大部分很简单,先贴一下代码. 1111 T1 //string //by Cydiater //2016.11.11 #include ...
- 2021.11.11 EXKMP
2021.11.11 EXKMP https://www.luogu.com.cn/problem/P5410 下标以1开头: #include<cstdio> #include<i ...
- 11.11光棍节工作心得——github/MVP
11.11光棍节工作心得 1.根据scrum meeting thirdday中前辈的指导进行学习 我在博客中贴了链接,竟然TrackBack引来了原博主,
- 【拓扑排序】【线段树】Gym - 101102K - Topological Sort
Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...
- topological sort~~~~初学
今天讲了topological sort 问题: 判环:记录入队的点数,若<n则有环,可证: 算法:o(n):queue or stack,而不是o(n^2)枚举 #. 关系运算图(vijos ...
- 下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y),y++);
下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y) ...
- topological sort
A topological sortof a dag G is a linear ordering of all its vertices such that if G contains anedg ...
- Hadoop格式化 From hu-hadoop1/192.168.11.11 to hu-hadoop2:8485 failed on connection exception: java.net.
192.168.11.12:8485: Call From hu-hadoop1/192.168.11.11 to hu-hadoop2:8485 failed on connection excep ...
- 拓扑排序(Topological Sort)
Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap ...
随机推荐
- Cntlm 配置上网代理
下载安装Cntlm之后.仅仅须要改动cntlm.ini文件,提供身份认证必要的信息,然后以服务的方式启动cntlm就能够了. 在cntlm.ini中有例如以下几个重要的配置是可能须要改动的: User ...
- ES6中Class与export简单用法
一.Class ES6中的Class用法类似Java的Class用法,但class的本质是js一个function //定义类 class Person { //定义构造方法 constructor( ...
- [2019BUAA软工助教]助教学期总结
[2019BUAA软工助教]助教学期总结 一.量化自评 线上 博客点评:https://www.cnblogs.com/ChildishChange/MyComments.html 共 106 条 博 ...
- C语言中的scanf与scanf_s 以及循环输入的问题解决
Scanf 在标准C中,scanf提供了键盘输入功能. scanf函数是一个标准库函数,它的函数原型在头文件“stdio.h”中.与printf函数相同,C语言也允许在使用scanf函数之前不必包含s ...
- Freemarker入门Demo
1:工程引入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId>freemark ...
- tensorflow之tf.stop_gradient
停止梯度计算. 当在一个图中执行时, 这个op按原样输出它的输入张量. 当构建ops来计算梯度时,该op会阻止将其输入贡献考虑在内. 参数: Input: 一个张量. name: 操作的名称(可选) ...
- jQuery 源码解析(三) pushStack方法 详解
该函数用于创建一个新的jQuery对象,然后将一个DOM元素集合加入到jQuery栈中,最后返回该jQuery对象,有三个参数,如下: elems Array类型 将要压入 jQuery 栈的数组元素 ...
- GitFirstRemote
1.$ git ls-remote From git@github.com:Smoothfu/WPFITEMSSOURCEPRODUCTCOLLECTION.git9a6669a2e2c9e22b30 ...
- C# 处理接口返回的XML格式数据
using System.Xml; //引入命名空间 //模拟接口返回的数据 string str=@"<JZD_Message xmlns:xsd=""http: ...
- Java之路---Day16(泛型)
2019-11-02-23:25:26 目录 1.泛型的概念: 2.泛型的定义和使用: 2.1定义和使用含有泛型的类: 2.2定义和使用含有泛型的方法: 2.3定义和使用含有泛型的接口: 泛型的概念: ...