BiruLyu
5/27/2017 - 7:30 PM

## 210. Course Schedule II(#1 Topological Sort).java

``````public class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {

for(int i = 0; i < numCourses; i++){
}

for(int i = 0; i < prerequisites.length; i++){
int start = prerequisites[i][0];
int end = prerequisites[i][1];
}

int[] explored = new int[numCourses];
//int[] res = null;
for(int i = 0; i < numCourses; i++){
if(explored[i] == 2){
continue;
}
return new int[0];
}
}

int[] res = new int[numCourses];

for(int i = 0; i < resTemp.size(); i++){
res[i] = resTemp.get(i);
}
return res;
}

explored[cur] = 1;
for(int i = 0; i < adjList.get(cur).size(); i++){
if(explored[temp] == 1) {
return true;
} else if(explored[temp] == 0) {
return true;
}
}
}
explored[cur] = 2;
return false;
}
}``````
``````class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
int[] indegree = new int[numCourses];
for (int i = 0; i < numCourses; i++) {
}
for (int[] e : prerequisites) {
indegree[e[0]]++;
}
for (int i = 0; i < numCourses; i++) {
if (indegree[i] == 0) {
q.offer(i);
}
}
int[] res = new int[numCourses];
int k = 0;
while (!q.isEmpty()) {
int cur = q.poll();
res[k++] = cur;
for (int i : adjList.get(cur)) {
if (--indegree[i] == 0) {
q.offer(i);
}
}
}
return k == numCourses ? res : new int[0];
}
}``````