[leedcode 207] Course Schedule

There are a total of n courses you have to take, labeled from 0 to n - 1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?

For example:

2, [[1,0]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.

2, [[1,0],[0,1]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.

public class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        //本题是求拓扑图,验证图是否有环,从叶子节点开始求(出度为0的点),如果所有点都能遍历到,则满足条件
        //需要辅助数组,数组的下标代表课程编号,数组的值代表出度
        //queue保存的是出度为0的点,每次向里面添加需要计算个数count,最后结果需要判断count是否等于课程数numCourses
        int []flag=new int[numCourses];
        for(int i=0;i<prerequisites.length;i++){

            flag[prerequisites[i][1]]++;
        }
        LinkedList<Integer> queue=new LinkedList<Integer>();
        int count=0;
        for(int i=0;i<numCourses;i++){
            if(flag[i]==0){//出度为0
                queue.add(i);
                count++;
            }
        }
        while(!queue.isEmpty()){
            int k=queue.remove();
            for(int i=0;i<prerequisites.length;i++){
                if(k==prerequisites[i][0]){
                    int l=prerequisites[i][1];
                    flag[l]--;
                    if(flag[l]==0){
                        count++;
                        queue.add(l);
                    }
                }

            }

        }
        return count==numCourses;

    }
}
时间: 2024-10-03 21:41:17

[leedcode 207] Course Schedule的相关文章

【LeetCode】207. Course Schedule

Course Schedule There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of

leetcode 207. Course Schedule 课程计划 ---------- java

There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and a l

[leedcode 210] Course Schedule II

There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and a l

207. Course Schedule

拓扑排序,算法如下: 1 L -> 排好序的点 2 S -> 所有没有incoming的点 3 当S不为空时: 4 从S中拿出一个点n 5 把n加入L 6 对于每个由n->m得到节点m: 7 把这条边从图里删除 8 如果m也没有incoming的边了 9 把m加入S 10 如果图中仍然有边 11 返回error(图中至少有一个环) 12 否则 13 返回L 1 public boolean canFinish(int numCourses, int[][] prerequisites)

LeetCode 207. Course Schedule(拓扑排序)

题目 There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and

leetcode 207. Course Schedule

https://leetcode.com/problems/course-schedule/#/description 图的算法题 题目就是求一堆课程有先修和后修的区别,这些课程能否构成拓扑排序而不矛盾: 应该算是我做的第一个关于图的算法题,一开始毫无思路,看了网上的sloutions,发现这道题还是比较简单的,可以用bfs和dfs都可以做.感觉还是比较典型的关于图算法例题. http://www.cnblogs.com/zmyvszk/p/5573234.html 这个题解讲的还是比较明白的,

Java for LeetCode 207 Course Schedule 【Unsolved】

There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and a l

LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)

求有向图中是否有环. 法一:拓扑排序 用一个队列维护所有入度为0的节点,每次弹出一个节点v,查看从v可达的所有节点u; 将u的入读减一,若u的入度此时为0, 则将u加入队列. 在队列为空时,检查所有节点的入度,若所有节点入度都为0, 则存在这样的一个拓扑排序 -- 有向图中不存在环. 代码: class Solution { public: bool canFinish(int numCourses, vector<pair<int, int>>& prerequisite

(medium)LeetCode 207.Course Schedule

There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and a l