forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseSchedule_207.java
More file actions
52 lines (42 loc) · 1.37 KB
/
CourseSchedule_207.java
File metadata and controls
52 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;
}
}