LeetCode 207 Course Schedule

原创 2015年07月09日 11:54:18

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.

Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

这道题其实就是检查图有没有环,检查图有没有环比较常用的方法就是使用拓扑排序,拓扑排序的思路就是不断从图中取出入度为0的点,直到不能没有入度为0的点,这样操作之后如果可以将图中所有的点全部取出,那么说明这个图是可以进行拓扑排序的,本题的具体的方法如下流程:
1. 找到入度为0的节点,入队
2. 队列是否为空
2.1 为空则跳出循环
2.2 不为空则取出一个节点,取出的记录数+1,作为当前点,遍历该当前点的所有邻接点,并将所有节点的入度均-1,表示取出了当前点,如果有节点的入度-1之后变为0,那么将其入队。
3. 取出的节点是否等于总的节点数
3.1 等于则说明没有环
3.2不等于则说明有环

具体代码如下:

class Solution {
public:
    bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
         vector<vector<int> > relation(numCourses);
         int size = prerequisites.size();
         vector<int> indegree(numCourses);
         int outnum = 0;
         for(int i = 0; i < size; i++)
         {
             pair<int, int> pairtmp = prerequisites[i];
             relation[pairtmp.first].push_back(pairtmp.second);
             indegree[pairtmp.second]++;
         }
         stack<int> wait;
         for(int i = 0; i < numCourses; i++)
         {
             if(indegree[i] == 0)
                wait.push(i);
         }
         while(!wait.empty())
         {
             int cur = wait.top();
             wait.pop();
             outnum++;
             for(int i = 0; i < relation[cur].size(); i++)
             {
                 int adject = relation[cur][i];
                 indegree[adject]--;
                 if(indegree[adject] == 0)
                    wait.push(adject);
             }
         }
         if(outnum == numCourses)
            return true;
        else
            return false;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[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 ...

LeetCode题解(Week5):207. Course Schedule

原题目There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have pr...

【LeetCode】207. Course Schedule判断有向图是否有环

1、问题描述输入:n, [[1,0], [2,1], …., [n, n-1], [0,1], …..],假设没有重复的边 n表示总共0~n-1门课程,即n个节点 [1,0]表示先上课程0,才能上...

LeetCode 207 - Course Schedule

一、问题描述Description:There are a total of n courses you have to take, labeled from 00 to n−1n-1.Some co...

LeetCode #207 - Course Schedule - Medium

Course Schedule Series Course Schedule I: Course Schedule II: ProblemThere are a total of n...

[Leetcode] 207. Course Schedule 解题报告

题目: There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may h...

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

求有向图中是否有环。 法一:拓扑排序 用一个队列维护所有入度为0的节点,每次弹出一个节点v,查看从v可达的所有节点u; 将u的入读减一,若u的入度此时为0, 则将u加入队列。 在队列为空时,检查所有...

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 prere...

[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...

LeetCode-207/210. Course Schedule (JAVA) (课程安排)

LeetCode-207/210. Course Schedule (JAVA) (课程安排)
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)