207. Course Schedule

class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        int[] indegrees=new int[numCourses];
        int[][] graph=new int[numCourses][numCourses];
        for(int i=0;i<prerequisites.length;i++)
        {
            if(graph[prerequisites[i][1]][prerequisites[i][0]]==0)
                indegrees[prerequisites[i][0]]++;
            graph[prerequisites[i][1]][prerequisites[i][0]]=1;
        }

        int count=0;
        Queue<Integer> que=new LinkedList<Integer>();
        for(int i=0;i<indegrees.length;i++)
            if(indegrees[i]==0)
                que.add(i);
        
        while(!que.isEmpty())
        {
            int c = que.poll();
            count++;
            for(int i=0;i<numCourses;i++)
                if(graph[c][i]!=0)
                {
                    indegrees[i]--;
                    if(indegrees[i]==0)
                        que.add(i);
                }
        }
        return count==numCourses;
    }
}

  

转载于:https://www.cnblogs.com/asuran/p/7796570.html

上一篇:定时任务调度在 Laravel 中的实现


下一篇:准确地在一分钟内触发Python函数