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;
    }
}

  

posted @ 2017-11-07 02:17  Weiyu Wang  阅读(135)  评论(0编辑  收藏  举报