当前位置:网站首页>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;
边栏推荐
- Distributed transaction solutions and code implementation
- Jetson Nano 从入门到实战(案例:Opencv配置、人脸检测、二维码检测)
- F - spices (linear basis)
- Please run IDA with elevated permissons for local debugging.
- 02-Epicor二次开发常用代码
- ARM汇编中的栈桢小结
- 【直播回顾】战码先锋第七期:三方应用开发者如何为开源做贡献
- beescms网站渗透测试和修复意见「建议收藏」
- 如何选择正规安全的外汇交易平台?
- 算力服务网络:一场多元融合的系统革命
猜你喜欢

Hashcat 的使用

转行软件测试2年了,给还在犹豫的女生一点建议

Please run IDA with elevated permissons for local debugging.

Can automate - 10k, can automate - 20K, do you understand automated testing?

Sumati gamefi ecological overview, element design in the magical world

It's 2022, and you still don't know what performance testing is?

EasyCVR国标协议接入的通道,在线通道部分播放异常是什么原因?

Processon producer process (customized)

进入阿里做测试员遥不可及?这里或许有你想要的答案

3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?
随机推荐
【第26天】给定 n 个元素的升序数组nums,求实现一个函数在nums中寻找target的下标 | 初识二分查找
How to quickly familiarize yourself with the code when you join a new company?
How to get the picture outside the chain - Netease photo album [easy to understand]
ProcessOn制作ER过程(自定义)
1-6搭建Win7虚拟机环境
Redis
The ecosystem of the yuan universe
jwt
业务与技术双向结合构建银行数据安全管理体系
[day 26] given the ascending array nums of n elements, find a function to find the subscript of target in nums | learn binary search
Software testing salary in first tier cities - are you dragging your feet
Are programmers from Huawei, Alibaba and other large manufacturers really easy to find?
云原生数据库VS传统数据库
【STL源码剖析】STL六大组件功能与运用(目录)
June 24, 2022: golang multiple choice question, what does the following golang code output? A:1; B:3; C:4; D: Compilation failed. package main import ( “f
How to monitor the log through the easycvr interface to observe the platform streaming?
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(3)—— 把数据库设置为归档模式
Can automate - 10k, can automate - 20K, do you understand automated testing?
当他们在私域里,掌握了分寸感
Four characteristics of actual attack and defense drill