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 ...
随机推荐
- python接口自动化4-常用取token值方法
前言 在接口测试中我们经常是需要一个登陆token,或者获取其他用到的参数来关联下一个接口用到的参数.这里介绍一些本人常用的方法. 一.简介 不过在哪里我们也是能实现自动化api测试的,我们都知道to ...
- Ubuntu 16.04上anaconda安装和使用教程,安装jupyter扩展等 | anaconda tutorial on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/23014ca5/,欢迎阅读最新内容! anaconda tutorial on ubuntu 16.04 Guide versio ...
- efcore mysql数据库codefirst生成
添加引用 Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.Tools Pomelo.EntityFrameworkCore.My ...
- python3之利用字典和列表实现城市多级菜单
利用字典和列表实现城市多级菜单 #coding:utf-8 #利用字典和列表实现城市多级菜单 addrIndex = {":"福建"} addrDict = {" ...
- LeetCode 905. Sort Array By Parity 按奇偶校验排列数组
题目 Given an array A of non-negative integers, return an array consisting of all the even elements of ...
- JS打开url的几种方法
在新标签页中打开 window.open(loginurl_withaccout, "_blank"); 下图中根据后台返回的url以及用户名密码字段,以及用户名密码动态生成了带账 ...
- java--正则校验
java--正则校验 // boolearn matches(String regex):判断当前字符串是否匹配指定的正则表达式true/false demo: String qq = "1 ...
- mysql建库,建表,补列
SET NAMES UTF8;DROP DATABASE IF EXISTS tmooc; CREATE DATABASE tmooc CHARSET=UTF8; USE tmooc;CREATE T ...
- influxdb-1.7.8(centos 7) 部署
1.官方下载: https://dl.influxdata.com/influxdb/releases/influxdb-1.7.8.x86_64.rpm 2.安装软件 sudo yum locali ...
- linux epoll,poll,select
epoll函数用法,还有点poll和select 1,LT的epoll是select和poll函数的改进版. 特点是,读完缓冲区后,如果缓冲区还有内容的话,epoll_wait函数还会返回,直到把缓冲 ...