GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaInterview/java/src/LeetCode/CourseSchedule_207.java at master · damaohongtu/JavaInterview Expand file tree/ Copy pathCourseSchedule_207.javaMore file actions 52 lines (42 loc) · 1.37 KB / Copy pathCourseSchedule_207.javaFile metadata and controls52 lines (42 loc) · 1.37 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 package LeetCode; import java.util.ArrayList; import java.util.List; /** * @Classname CourseSchedule_207 * @Description 有先验课程,判断可以完成的课程 * 思路:使用的是邻接矩阵 * @Date 19-5-21 上午9:56 * @Created by mao<[email protected]> */ public class CourseSchedule_207 { public boolean canFinish(int numCourses, int[][] prerequisites) { List<Integer>[] graph = new List[numCourses]; for (int i = 0; i < numCourses; i++) graph[i] = new ArrayList<Integer>(); //建立邻接表 for (int i = 0; i < prerequisites.length; i++) { graph[prerequisites[i][1]].add(prerequisites[i][0]); } int[] visited = new int[numCourses]; for (int i = 0; i < numCourses; i++) { //找到一个环路,就完成不了课程 if (hasLoop(graph, visited, i)) return false; } return true; } private boolean hasLoop(List<Integer>[] graph, int[] visited, int course) { visited[course] = 1; for (int depend : graph[course]) { //课程 if (visited[depend] == 1) return true; else if (visited[depend] == 0) { if (hasLoop(graph, visited, depend)) return true; } } visited[course] = 2; return false; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |