知之可否

Be yourself; everyone else is already taken.​

拓扑排序：课程表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.

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

class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {

vector<int> inDgree(numCourses,0);

int len = prerequisites.size();
for (int i = 0; i < len; i++){
pair<int, int> p = prerequisites[i];

inDgree[p.first]++;
}
}

queue<int> Q;

for (int i=0; i < numCourses; i++){
if (inDgree[i] == 0)
Q.push(i);
}

while (!Q.empty()){
int front = Q.front();
Q.pop();

inDgree[i]--;
if (inDgree[i] == 0){
Q.push(i);
}
}

}

for (int i: inDgree){
if (i)
return false;
}
return true;
}

};


Leetcode 207 Course Schedule 课程表

2015-08-10 18:05:39

【LeetCode】Course Schedule 解题报告

2015-05-19 17:05:08

leetcode-Course Schedule

2015-05-06 23:40:53

[leetcode] 210. Course Schedule II 解题报告

2016-02-26 14:22:04

leetcode Course Schedule

2015-08-04 11:40:57

拓扑排序 ---排课表----数据结构

2010年06月13日 414KB 下载

数据结构学习（三）：拓扑排序----课程表

2018-06-07 15:24:15

207. Course Schedule

2016-07-30 23:33:54

拓扑排序：课程表Course Schedule2

2016-05-31 16:44:39

lintcode(616)安排课程

2017-05-18 09:59:26