当前位置:网站首页>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;
边栏推荐
- 商城项目 pc----商品详情页
- Multimodal emotion recognition_ Research on emotion recognition based on multimodal fusion
- 3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?
- 指南针靠谱吗?开证券账户安全吗?
- LINQ 查询(3)
- mysql命令备份
- [mobile terminal] design size of mobile phone interface
- yarn : 无法加载文件 C:\Users\xxx\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本
- 計網 | 【四 網絡層】知識點及例題
- Talking about the advantages of flying book in development work | community essay solicitation
猜你喜欢
一线城市软件测试工资——你拖后腿了吗
都2022年了,你还不了解什么是性能测试?
Cusdis - 轻量级、隐私优先的开源评论系统 | 倾城之链
EasyCVR国标协议接入的通道,在线通道部分播放异常是什么原因?
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source
【直播回顾】战码先锋第七期:三方应用开发者如何为开源做贡献
转行软件测试2年了,给还在犹豫的女生一点建议
What is the reason for the disconnection of video playback due to the EHOME protocol access of easycvr platform?
EasyCVR平台EHOME协议接入,视频播放出现断流是什么原因?
左手梦想 右手责任 广汽本田不光关注销量 还有儿童安全
随机推荐
Multimodal emotion recognition_ Research on emotion recognition based on multimodal fusion
内网学习笔记(7)
Is the compass reliable? Is it safe to open a securities account?
Sumati gamefi ecological overview, element design in the magical world
元宇宙的生态圈
EasyCVR国标协议接入的通道,在线通道部分播放异常是什么原因?
【第26天】给定 n 个元素的升序数组nums,求实现一个函数在nums中寻找target的下标 | 初识二分查找
psql 列转行
The ecosystem of the yuan universe
Application of TSDB in civil aircraft industry
云原生数据库VS传统数据库
DDD concept is complex and difficult to understand. How to design code implementation model in practice?
分布式事务解决方案和代码落地
Is it out of reach to enter Ali as a tester? Here may be the answer you want
Squid 代理服务器之 ACL 访问控制
Processon producer process (customized)
How to monitor the log through the easycvr interface to observe the platform streaming?
02-Epicor二次开发常用代码
vim的Dirvish中文文档
ARM汇编中的栈桢小结