1 Star 0 Fork 0

Neo_1001/LeetCode刷题记录

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
LC_207.java 1.18 KB
Copy Edit Raw Blame History
Neo_1001 authored 2024-04-10 22:16 . first commit
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;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/neo1001/leet-code-practice-record.git
git@gitee.com:neo1001/leet-code-practice-record.git
neo1001
leet-code-practice-record
LeetCode刷题记录
master

Search