首页 > 代码库 > [LeetCode] 207. Course Schedule
[LeetCode] 207. Course Schedule
https://leetcode.com/problems/course-schedule
public class Solution { public boolean canFinish(int numCourses, int[][] prerequisites) { int[][] matrix = new int[numCourses][numCourses]; boolean[] onpath = new boolean[numCourses]; boolean[] visited = new boolean[numCourses]; boolean isCycle = false; for (int i = 0; i < prerequisites.length; i++) { int cur = prerequisites[i][0]; int pre = prerequisites[i][1]; matrix[cur][pre] = 1; } for (int i = 0; i < numCourses; i++) { isCycle = isCycle || dfs_cycle(matrix, onpath, visited, i); } return !isCycle; } private boolean dfs_cycle(int[][] matrix, boolean[] onpath, boolean[] visited, int node) { if (visited[node]) { return false; } onpath[node] = true; visited[node] = true; for (int j = 0; j < matrix[node].length; j++) { if (matrix[node][j] == 1) { if (onpath[j] || dfs_cycle(matrix, onpath, visited, j)) { return true; } } } onpath[node] = false; return false; } }
[LeetCode] 207. Course Schedule
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。