# JAVA---------Easy Version To UnderStand!!!!!!!!!!!!!!!!!

• ``````public static boolean canFinish(int numCourses, int[][] prerequisites) {
if (numCourses <= 0)
return false;
int[] inDegree = new int[numCourses];
for (int i = 0; i < prerequisites.length; i++) {
inDegree[prerequisites[i][1]]++;
}
for (int i = 0; i < inDegree.length; i++) {
if (inDegree[i] == 0)
queue.offer(i);
}
while (!queue.isEmpty()) {
int x = queue.poll();
for (int i = 0; i < prerequisites.length; i++) {
if (x == prerequisites[i][0]) {
inDegree[prerequisites[i][1]]--;
if (inDegree[prerequisites[i][1]] == 0)
queue.offer(prerequisites[i][1]);
}
}
}
for (int i = 0; i < inDegree.length; i++) {
if (inDegree[i] != 0)
return false;
}
return true;
}``````

• Thanks, this solution is very good!

• thanks,please vote me for this problem!!!

• Line 7:
is inDegree[prerequisites[i][1]]++; correct?
i think it should be inDegree[prerequisites[i][0]]++;

• Can anybody explain to me why this solution work? Are there any theories about the indegree and outdegree which support this solution?

• @alpit I think you are right. I am confused for a long time.

• Very easy to understand, except for the mistake which @alpit already mentioned above. However the Time complexity is not good.

• clear and easy to understand!!!!!

• Why should we start with the number whose inDegree is zero?

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.