当前位置:网站首页>LeetCode 210:课程表 II (拓扑排序)
LeetCode 210:课程表 II (拓扑排序)
2022-06-24 23:01:00 【斯沃福德】
题目:
思路:
拓扑排序理论
仅在判断是否成环基础上增加了一个list用于记录
注意:
- 递归到头才开始记录,所以是后续遍历
- Queue和Stack都有size()方法,继承于Collection和Vector
- 终止条件在前,onPath在marked之前!
class Solution {
boolean isCycle=false;
List<Integer> list=new ArrayList<>();
boolean[] marked;
boolean[] onPath;
public int[] findOrder(int numCourses, int[][] prerequisites) {
List<Integer>[] graph=buildGraph(prerequisites,numCourses);
marked=new boolean[numCourses];
onPath=new boolean[numCourses];
for(int i=0;i<numCourses;i++){
//遍历顶点
dfs(graph,i);
}
if(isCycle){
return new int[]{
};
}
int n=list.size();
int[] r=new int[n];
Collections.reverse(list);
for(int i=0;i<n;i++){
r[i]=list.get(i);
}
return r;
}
List<Integer>[] buildGraph(int[][] prerequisites,int numCourses){
List<Integer>[] graph=new List[numCourses];
for(int i=0;i<numCourses;i++){
graph[i]=new LinkedList<Integer>();
}
for(int[] k:prerequisites){
int pre=k[1];
int follow=k[0];
graph[pre].add(follow);
}
return graph;
}
void dfs(List<Integer>[] graph,int s){
//终止条件, onPath在marked 之前 !
if(onPath[s]){
isCycle=true;
return;
}
if(marked[s]){
return ;
}
marked[s]=true;
onPath[s]=true;
for(int k:graph[s]){
//遍历邻接表
dfs(graph,k);
}
list.add(s); // 后续遍历 !
onPath[s]=false;
}
}
或者用栈来记录:
int n=stack.size();
int[] r=new int[n];
for(int i=0;i<n;i++){
r[i]=stack.pop();
}
return r;
边栏推荐
- vim的Dirvish中文文档
- mysql命令备份
- When an interface has an exception, how do you analyze the exception?
- What are the reasons for the abnormal playback of the online channel of the channel accessed by easycvr national standard protocol?
- 如何通过EasyCVR接口监测日志观察平台拉流情况?
- internship:svn的使用
- 内网学习笔记(6)
- Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (4) -- modify the scanip of Oracle11g RAC cluster
- Cusdis - lightweight, privacy first open source comment system | chain of the city
- 计网 | 【四 网络层】知识点及例题
猜你喜欢
![[live review] battle code pioneer phase 7: how third-party application developers contribute to open source](/img/ad/26a302ca724177e37fe123f8b75e4e.png)
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source

File system - basic knowledge of disk and detailed introduction to FAT32 file system

入坑机器学习:一,绪论

ProcessOn制作ER过程(自定义)

AI服装生成,帮你完成服装设计的最后一步

Of the seven levels of software testers, it is said that only 1% can achieve level 7

做软件安全测试的作用,如何寻找软件安全测试公司出具报告?

Random list random generation of non repeating numbers

测试/开发程序员,30而立,你是否觉得迷茫?又当何去何从......

Redis
随机推荐
Cusdis - lightweight, privacy first open source comment system | chain of the city
jwt
02-Epicor二次开发常用代码
进入阿里做测试员遥不可及?这里或许有你想要的答案
都2022年了,你还不了解什么是性能测试?
3年测试经验,连简历上真正需要什么都没搞明白,张口就要20k?
Pit entry machine learning: I. Introduction
Rod and Schwartz cooperated with ZhongGuanCun pan Lianyuan Institute to carry out 6G technology research and early verification
Hashcat 的使用
Explanation of FTP protocol
Intranet learning notes (5)
当他们在私域里,掌握了分寸感
[mobile terminal] design size of mobile phone interface
计网 | 【四 网络层】知识点及例题
调用系统函数安全方案
【STL源码剖析】配置器(待补充)
Four characteristics of actual attack and defense drill
It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets
左手梦想 右手责任 广汽本田不光关注销量 还有儿童安全
当人们用互联网式的思维和视角来看待产业互联网的时候,其实已陷入到了死胡同