亚麻:Course Schedule II

 

这是亚麻的OA题,

leetcode链接。https://leetcode.com/problems/course-schedule-ii/description/

the code listed here has passed all the test case in leetcode.

 


// key point
// use the vector< pre, vector<independents courses> > to create graph
// use vector< indegree > to record the node of the incomming degrees
// use queue to BFS.

vector<int> res; vector<vector<int> > graph(numCourses, vector<int>(0)); vector<int> in(numCourses, 0); for (auto &a : prerequisites) { graph[a.second].push_back(a.first); ++in[a.first]; } queue<int> q; for (int i = 0; i < numCourses; ++i) { if (in[i] == 0) q.push(i); } while (!q.empty()) { int t = q.front(); res.push_back(t); q.pop(); for (auto &a : graph[t]) { --in[a]; if (in[a] == 0) q.push(a); } } if (res.size() != numCourses) res.clear(); return res;

 

posted @ 2018-01-17 08:58  HisonSanDiego  阅读(175)  评论(0编辑  收藏  举报