-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path207-classTable.java
More file actions
40 lines (39 loc) · 1.71 KB
/
207-classTable.java
File metadata and controls
40 lines (39 loc) · 1.71 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
import java.util.*;
class Solution_207 {
// 有向图无环的问题,这x个也是拓扑排序的例子
public boolean canFinish(int numCourses, int[][] prerequisites) {
if(numCourses == 0) return true;
int[] dp = new int[numCourses];
Queue<Integer> queue = new LinkedList<Integer>();
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
// 存入度和临接表
for(int i=0; i<prerequisites.length; i++) {
dp[prerequisites[i][0]] += 1;
ArrayList<Integer> temp = map.get(prerequisites[i][1]);
if(temp == null) {
temp = new ArrayList<Integer>();
temp.add(prerequisites[i][0]);
map.put(prerequisites[i][1], temp);
} else {
temp.add(prerequisites[i][0]);
}
}
// 有哪些入口
for(int i=0; i<numCourses; i++) {
if(dp[i] == 0) {queue.add(i);}
}
// 宽度优先遍历
while(queue.peek()!=null) {
int val = queue.poll();
if(map.get(val) == null) continue;
for(int i=0; i<map.get(val).size(); i++) {
dp[map.get(val).get(i)]--;
if(dp[map.get(val).get(i)] == 0) queue.add(map.get(val).get(i));
}
}
for(int i=0; i<numCourses; i++) {
if(dp[i] != 0) return false;
}
return true;
}
}