210. Course Schedule II
class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { int[] indegrees=new int[numCourses]; int[][] graph=new int[numCourses][numCourses]; for (int i=0;i<prerequisites.length;i++) { if(graph[prerequisites[i][1]][prerequisites[i][0]]==0) indegrees[prerequisites[i][0]]++; graph[prerequisites[i][1]][prerequisites[i][0]]=1; } Queue<Integer> que=new LinkedList<Integer>(); for(int i=0;i<numCourses;i++) if(indegrees[i]==0) que.add(i); List<Integer> res=new ArrayList<Integer>(); while(!que.isEmpty()) { int c=que.poll(); res.add(c); for(int i=0;i<numCourses;i++) { if(graph[c][i]==1) { indegrees[i]--; if(indegrees[i]==0) que.add(i); } } } if(res.size()!=numCourses) return new int[0]; int[] ret=new int[res.size()]; for(int i=0;i<numCourses;i++) ret[i]=res.get(i); return ret; } }