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

  1. NOIp 11.11/12

    最后一场比较正式的NOIp模拟赛,写一发小总结.题目没什么好说的,大部分很简单,先贴一下代码. 1111 T1 //string //by Cydiater //2016.11.11 #include ...

  2. 2021.11.11 EXKMP

    2021.11.11 EXKMP https://www.luogu.com.cn/problem/P5410 下标以1开头: #include<cstdio> #include<i ...

  3. 11.11光棍节工作心得——github/MVP

    11.11光棍节工作心得 1.根据scrum meeting thirdday中前辈的指导进行学习 我在博客中贴了链接,竟然TrackBack引来了原博主,

  4. 【拓扑排序】【线段树】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 ...

  5. topological sort~~~~初学

    今天讲了topological sort 问题: 判环:记录入队的点数,若<n则有环,可证: 算法:o(n):queue or  stack,而不是o(n^2)枚举 #. 关系运算图(vijos ...

  6. 下面程序的输出结果是____ 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) ...

  7. topological sort

    A topological sortof a dag G  is a linear ordering of all its vertices such that if G contains anedg ...

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

  9. 拓扑排序(Topological Sort)

    Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap ...

随机推荐

  1. Qt 删掉资源qss后报错

    Error: dependent '..\..\........XXXX.qss' does not exist. 解决方案: 1.清理工程 2.qmake 3.重新构建

  2. 剑指offer:二叉搜索树的第k个结点(中序遍历)

    1. 题目描述 /* 给定一棵二叉搜索树,请找出其中的第k小的结点. 例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. */ 2. 思路 中序遍历二叉搜索树,第K个就 ...

  3. java架构之路(多线程)JMM和volatile关键字

    说到JMM大家一定很陌生,被我们所熟知的一定是jvm虚拟机,而我们今天讲的JMM和JVM虚拟机没有半毛钱关系,千万不要把JMM的任何事情联想到JVM,把JMM当做一个完全新的事物去理解和认识. 我们先 ...

  4. .NET Core 使用NPOI读取Excel返回泛型List集合

    我是一名 ASP.NET 程序员,专注于 B/S 项目开发.累计文章阅读量超过一千万,我的博客主页地址:https://www.itsvse.com/blog_xzz.html 网上有很多关于npoi ...

  5. C#工具类SqlServerHelper,基于System.Data.SqlClient封装

    源码: using System; using System.Collections.Generic; using System.Data; using System.Linq; using Syst ...

  6. WinForms中动态给treeView的节点添加ContextMenuStrip,并绑定Click事件

    生成ContextMenuStrip var docMenu = new ContextMenuStrip(); ToolStripMenuItem deleteMenuItem = new Tool ...

  7. 在Linux系统中运行并简单的测试RabbitMq容器

    以前使用的是Windows下面的RabbitMq,需要先安装 Erlang的语言环境等,这次直接在Linux中的Docker容器来测试一下 1:docker配置RabbitMq的指令 docker r ...

  8. Java生鲜电商平台-商品无限极目录的设计与架构

    Java生鲜电商平台-商品无限极目录的设计与架构 说明:任何一个商品都应该是先属于某一个目录,然后在目录中添加商品,目录理论上最多支持三级,因为级别太多,不容易管理.但是设计中需要设计无限制的级别. ...

  9. Java生鲜电商平台-物流配送的设计与架构

    Java生鲜电商平台-物流配送的设计与架构 说明:由于Java开源生鲜电商平台是属于自建物流系统,也就是买家下的单,需要公司派物流团队进行派送.            业务需求中买家的下单时间控制在: ...

  10. Django的路由系统:URL

    一. URLconf配置 基本格式 from django.conf.urls import url urlpatterns = [ url(正则表达式, views视图,参数,别名), ] 参数说明 ...