GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Leetcode-Java/leetcode/solution/src/CourseScheduleII.java at master · Tulpen/Leetcode-Java Expand file tree/ Copy pathCourseScheduleII.javaMore file actions 38 lines (36 loc) · 1.11 KB / Copy pathCourseScheduleII.javaFile metadata and controls38 lines (36 loc) · 1.11 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 import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Set; public class CourseScheduleII { public int[] findOrder(int numCourses, int[][] prerequisites) { int[] indegree = new int[numCourses]; Set<Integer>[] sets = new HashSet[numCourses]; for (int i = 0; i < sets.length; i++) { sets[i] = new HashSet<>(); } for (int[] pre : prerequisites) { if (sets[pre[1]].add(pre[0])) { indegree[pre[0]]++; } } Queue<Integer> queue = new LinkedList<>(); for (int i = 0; i < numCourses; i++) { if (indegree[i] == 0) { queue.add(i); } } int count = 0; int[] result = new int[numCourses]; while (!queue.isEmpty()) { Integer n = queue.poll(); result[count++] = n; for (Integer k : sets[n]) { if (--indegree[k] == 0) { queue.add(k); } } } return count == numCourses ? result : new int[0]; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |