Fetch the repository succeeded.
package LeetCode;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
public class LC_207 {
static ArrayList<ArrayList<Integer>> list = new ArrayList<>();
static Deque<Integer> que = new ArrayDeque<Integer>();
public static void main(String[] args ) {
System.out.println(canFinish(3, new int[][]{{1,0},{0,2}}));
}
public static boolean canFinish(int numCourses, int[][] prerequisites) {
int[] indeg = new int[numCourses];
for (int i = 0; i < numCourses; i++) {
list.add(new ArrayList<Integer>());
}
for (int i = 0; i < prerequisites.length; i++) {
list.get(prerequisites[i][1]).add(prerequisites[i][0]);
indeg[prerequisites[i][0]]++;
}
for (int i = 0; i < indeg.length; i++) {
if (indeg[i] == 0)
que.add(i);
}
int count = 0;
while (!que.isEmpty()) {
count++;
int u = que.poll();
for (int v : list.get(u)) {
indeg[v]--;
if (indeg[v] == 0)
que.add(v);
}
}
return count == numCourses;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。