Skip to content

Commit 36a5454

Browse files
committed
fd
1 parent 547c75d commit 36a5454

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

solution/src/main/java/com/inuker/solution/CourseScheduleII.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,13 @@ public class CourseScheduleII {
1414

1515
public int[] findOrder(int numCourses, int[][] prerequisites) {
1616
int[] indegree = new int[numCourses];
17-
HashMap<Integer, Set<Integer>> map = new HashMap<>();
17+
Set<Integer>[] sets = new HashSet[numCourses];
18+
for (int i = 0; i < sets.length; i++) {
19+
sets[i] = new HashSet<>();
20+
}
1821
for (int[] pre : prerequisites) {
19-
int from = pre[1], to = pre[0];
20-
Set<Integer> set = map.get(from);
21-
if (set == null) {
22-
set = new HashSet<Integer>();
23-
map.put(from, set);
24-
}
25-
/**
26-
* 这里要避免添加多次
27-
*/
28-
if (set.add(to)) {
29-
indegree[to]++;
22+
if (sets[pre[1]].add(pre[0])) {
23+
indegree[pre[0]]++;
3024
}
3125
}
3226
Queue<Integer> queue = new LinkedList<>();
@@ -40,12 +34,9 @@ public int[] findOrder(int numCourses, int[][] prerequisites) {
4034
while (!queue.isEmpty()) {
4135
Integer n = queue.poll();
4236
result[count++] = n;
43-
Set<Integer> set = map.get(n);
44-
if (set != null) {
45-
for (Integer k : set) {
46-
if (--indegree[k] == 0) {
47-
queue.add(k);
48-
}
37+
for (Integer k : sets[n]) {
38+
if (--indegree[k] == 0) {
39+
queue.add(k);
4940
}
5041
}
5142
}

test/src/main/java/com/inuker/test/Test2.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,34 @@
2020

2121
public class Test2 {
2222

23-
public int countComponents2(int n, int[][] edges) {
24-
List<Integer>[] graph = new ArrayList[n];
25-
for (int i = 0; i < n; i++) {
26-
graph[i] = new ArrayList<>();
23+
public int[] findOrder(int numCourses, int[][] prerequisites) {
24+
int[] indegree = new int[numCourses];
25+
Set<Integer>[] sets = new HashSet[numCourses];
26+
for (int i = 0; i < sets.length; i++) {
27+
sets[i] = new HashSet<>();
2728
}
28-
for (int[] edge : edges) {
29-
graph[edge[0]].add(edge[1]);
30-
graph[edge[1]].add(edge[0]);
31-
}
32-
Set<Integer> visited = new HashSet<>();
33-
int count = 0;
34-
for (int i = 0; i < n; i++) {
35-
if (!visited.contains(i)) {
36-
count++;
37-
dfs(graph, i, visited);
29+
for (int[] pre : prerequisites) {
30+
if (sets[pre[1]].add(pre[0])) {
31+
indegree[pre[0]]++;
3832
}
3933
}
40-
return count;
41-
}
42-
43-
private void dfs(List<Integer>[] graph, int i, Set<Integer> visited) {
44-
if (!visited.add(i)) {
45-
return;
34+
Queue<Integer> queue = new LinkedList<>();
35+
for (int i = 0; i < numCourses; i++) {
36+
if (indegree[i] == 0) {
37+
queue.add(i);
38+
}
4639
}
47-
for (Integer k : graph[i]) {
48-
dfs(graph, k, visited);
40+
int count = 0;
41+
int[] result = new int[numCourses];
42+
while (!queue.isEmpty()) {
43+
Integer n = queue.poll();
44+
result[count++] = n;
45+
for (Integer k : sets[n]) {
46+
if (--indegree[k] == 0) {
47+
queue.add(k);
48+
}
49+
}
4950
}
51+
return count == numCourses ? result : new int[0];
5052
}
5153
}

0 commit comments

Comments
 (0)