当前位置:网站首页>LeetCode 207:课程表(拓扑排序判断是否成环)
LeetCode 207:课程表(拓扑排序判断是否成环)
2022-06-24 06:44:00 【斯沃福德】
题目:
思路:
看到依赖问题,首先想到把问题转换为有向图 !
- 利用条件构建图,课程即顶点,几个节点就有几个顶点,以个数建立List[ ]数组表示图,数组的索引就是顶点的值,数组的每个元素都是顶点对应的邻接表;
用先学习的课程pre指向后学习的课程follow来表示有向图的顺序 - main中用第一个for遍历图的每个顶点,对顶点的邻接表使用dfs
- 在dfs中用第二个for遍历该顶点的邻接表
当onpath为真则标记isCycle为true
注意for结束后要还原 onpath=false
注意:
为便于记忆,当onpath[s]=true 和 marked[s]=true 都直接return
Java实现:
class Solution {
boolean[] marked;
boolean[] onPath;
boolean isCycle=false;
public boolean canFinish(int numCourses, int[][] prerequisites) {
// 不可能完成: 即存在循环依赖,互相为前提, 就不能完成课程
//构建图
List<Integer>[] graph=buildGraph(numCourses,prerequisites);
//初始化
marked=new boolean[numCourses];
onPath=new boolean[numCourses];
//用dfs遍历每个顶点
for(int i=0;i<numCourses;i++){
dfs(graph,i);
}
return !isCycle;//不成环即可以完成学习
}
List<Integer>[] buildGraph(int numCourses,int[][]prerequisites){
List<Integer>[] graph=new List[numCourses];//graph元素是数组!
//初始化每个元素:new一个List作为邻接表
for(int i=0;i<numCourses;i++){
graph[i]=new LinkedList<>();
}
//添加有向边
for(int[] k:prerequisites){
int pre=k[1];
int follow=k[0];
graph[pre].add(follow); // graph的索引对应顶点的值,
}
return graph;
}
}
void dfs(List<Integer>[] graph,int s){
//终止条件
if(onPath[s]==true){
isCycle=true;
return;
}
if(marked[s]==true){
return;
}
marked[s]=true;//标记当前s
onPath[s]=true;//标记onpath
//遍历邻接表
for(int k:graph[s]){
dfs(graph,k);
}
onPath[s]=false;//onpath还原
}
边栏推荐
- Maxcompute remote connection, uploading and downloading data files
- Knowledge points of 2022 system integration project management engineer examination: ITSS information technology service
- jarvisoj_ level2
- 光照使用的简单总结
- RDD基础知识点
- Muxvlan principle, Huawei MUX VLAN experimental configuration
- The fund management of London gold is more important than others
- [MySQL usage Script] clone data tables, save query data to data tables, and create temporary tables
- buuctf misc [UTCTF2020]docx
- 10 common malware detection and analysis platforms
猜你喜欢

PIP install XXX on the terminal but no module named XXX on pycharm

相機標定(標定目的、原理)

How can genetic testing help patients fight disease?

前缀和专题训练

Only two lines are displayed, and the excess part is displayed with Ellipsis

Analog display of the module taking software verifies the correctness of the module taking data, and reversely converts the bin file of the lattice array to display
![[MRCTF2020]千层套路](/img/8e/d7b6e7025b87ea0f43a6123760a113.png)
[MRCTF2020]千层套路
![[image feature extraction] image feature extraction based on pulse coupled neural network (PCNN) including Matlab source code](/img/b3/26cfa385aa357c3a7a77e9db47e94c.png)
[image feature extraction] image feature extraction based on pulse coupled neural network (PCNN) including Matlab source code

When MFC uses the console, the project path cannot have spaces or Chinese, otherwise an error will be reported. Lnk1342 fails to save the backup copy of the binary file to be edited, etc

Software performance test analysis and tuning practice path - JMeter's performance pressure test analysis and tuning of RPC Services - manuscript excerpts
随机推荐
[Proteus] Arduino uno + ds1307+lcd1602 time display
Fine! Storage knowledge is a must for network engineers!
使用SystemParametersInfo访问用户界面设置
相机标定(标定目的、原理)
图形技术之坐标转换
[WUSTCTF2020]alison_ likes_ jojo
Selector (>, ~, +, [])
Shell script for MySQL real-time synchronization of binlog
MFC multithreaded semaphore csemaphore critical area and mutually exclusive events
前缀和专题训练
buuctf misc [UTCTF2020]docx
Prefix and topic training
What should I pay attention to after the live broadcast system source code is set up?
buuctf misc 从娃娃抓起
图形技术之管线概念
MaxCompute远程连接,上传、下载数据文件操作
Black box and white box models for interpretable AI
阿里云全链路数据治理
How to open the soft keyboard in the computer, and how to open the soft keyboard in win10
What are the dazzling skills of spot gold?