classSolution { publicbooleancanFinish(int numCourses, int[][] prerequisites) { int[] indegrees = newint[numCourses]; // 入度表 List<List<Integer>> adjacency = newArrayList<>(); Queue<Integer> queue = newLinkedList<>(); for (inti=0; i < numCourses; i++) adjacency.add(newArrayList<>()); // Get the indegree and adjacency of every course. for (int[] cp : prerequisites) { indegrees[cp[0]]++; adjacency.get(cp[1]).add(cp[0]); } // Get all the courses with the indegree of 0. for (inti=0; i < numCourses; i++) if (indegrees[i] == 0) queue.add(i); // BFS TopSort. while (!queue.isEmpty()) { intpre= queue.poll(); numCourses--; for (int cur : adjacency.get(pre)) if (--indegrees[cur] == 0) queue.add(cur); } return numCourses == 0; } }