Public class numCourses {public static int[] findOrder(int numCourses, Int [][] prerequisites = new int[numCourses]; Int [] ans = new int[numCourses]; // How much is the degree of each point according to the coursefor(int[] edge : prerequisites) { input[edge[0]]++; } Queue<Integer> Queue = new LinkedList<>(); // The input degree is 0for (int i = 0; i < numCourses; i++) {
            if(input[i] == 0) { queue.offer(i); Int pos = 0; int pos = 0;while(! Queue. IsEmpty ()) {int temp = queue. Poll (); ans[pos++] = temp; // We will use temp as the prerequisite coursefor (int[] edge : prerequisites) {
                if (edge[1] == temp) {
                    input[edge[0]]--;
                    if(input[edge[0]] == 0) { queue.offer(edge[0]); }}}}if(pos ! = numCourses)return new int[]{};
        returnans; }}Copy the code