当前位置:网站首页>[daily training] 207 Class Schedule Card
[daily training] 207 Class Schedule Card
2022-06-25 07:49:00 【Puppet__】
subject
You must take this semester numCourses Courses , Write it down as 0 To numCourses - 1 .
Before you take some courses, you need some prerequisite courses . Prerequisite courses by array prerequisites give , among prerequisites[i] = [ai, bi] , If you want to learn a course ai be must Learn the course first bi .
for example , The prerequisite course is right for [0, 1] Express : Want to learn the course 0 , You need to finish the course first 1 .
Please judge whether it is possible to complete all the courses ? If possible , return true ; otherwise , return false .
Example 1:
Input :numCourses = 2, prerequisites = [[1,0]]
Output :true
explain : All in all 2 Courses . Study the course 1 Before , You need to complete the course 0 . It's possible .
Example 2:
Input :numCourses = 2, prerequisites = [[1,0],[0,1]]
Output :false
explain : All in all 2 Courses . Study the course 1 Before , You need to finish Course 0 ; And study the course 0 Before , You should also finish the course first 1 . It's impossible .
Tips :
1 <= numCourses <= 105
0 <= prerequisites.length <= 5000
prerequisites[i].length == 2
0 <= ai, bi < numCourses
prerequisites[i] All the courses in are right Different from each other
Code
package dayLeetCode;
import java.util.ArrayList;
import java.util.List;
public class dayleetcode207 {
// dfs
// Connect the account with its predecessor
List<List<Integer>> list = new ArrayList<>();
// Tag array ,0 Not searched , 1 Searching , 2 Search complete
int visited[];
// Mark whether there is a ring , A ring must be false
boolean flag = true;
public boolean canFinish(int numCourses, int[][] prerequisites) {
// initialization
visited = new int[numCourses];
for (int i = 0; i < numCourses; i++){
list.add(new ArrayList<Integer>());
}
for (int num[] : prerequisites){
// learn num[0] Before Want to learn num[1], So let num[1] Point to num[0]
list.get(num[0]).add(num[1]);
}
for (int i = 0; i < numCourses; i++){
if (visited[i]== 0){
dfs(i);
}
}
return flag;
}
private void dfs(int k) {
visited[k] = 1;
// To study subjects k Words , Need to finish list.get(k) These front accounts
for (int t : list.get(k)){
// If the subject pointed to has not been searched, search deeply
if (visited[t] == 0){
dfs(t);
// Determine whether the lower level search has a ring
if (!flag){
return;
}
}
// An account pointed to is in search , Generate rings
else if (visited[t] == 1){
flag = false;
return;
}
}
// Search complete , Mark as searched , Learning is over
visited[k] = 2;
}
public static void main(String[] args) {
dayleetcode207 obj = new dayleetcode207();
System.out.println(obj.canFinish(2, new int[][]{
{
1, 0}}));
}
}
边栏推荐
猜你喜欢

函数模板_类模板

Elk + filebeat log parsing, log warehousing optimization, logstash filter configuration attribute

The method of judging whether triode can amplify AC signal

搞清信息化是什么,让企业转型升级走上正确的道路

Modular programming of LCD1602 LCD controlled by single chip microcomputer

Chuantu microelectronics 𞓜 subminiature package isolated half duplex 485 transceiver

无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略

“空间转换”显著提升陡崖点云的地面点提取质量

STL tutorial 4- input / output stream and object serialization

用函数的递归来解决几道有趣的题
随机推荐
SSL证书免费获取教程
Share the process requirements for single-layer flexible circuit board
[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905
Cglib dynamic proxy
Sichuan Tuwei ca-if1051 can transceiver has passed aec-q100 grade 1 certification
lebel只想前面有星号,但是不想校验
双三次差值bicubic
(tool class) quickly add time to code in source insight
Storage of Galileo broadcast ephemeris in rtklib-b33
Can I open a stock account with a compass? Is it safe?
JDBC-DAO层实现
Mysql面试-执行sql响应比较慢,排查思路。
微信小程序开通客服消息功能开发
Summary of small problems in smartbugs installation
One "stone" and two "birds", PCA can effectively improve the dilemma of missing some ground points under the airborne lidar forest
useMemo模拟useCallback
[QT] shortcut key
海思3559 sample解析:vio
C#中如何调整图像大小
基于地面点稀少的LiDAR点云的茂密森林蓄积量估算