leetcode 210. Course Schedule II
Posted 炮二平五
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 210. Course Schedule II相关的知识,希望对你有一定的参考价值。
跟leetcode207大致相同,巩固一下把,不在赘述。ac代码
class Solution { public: vector<unordered_set<int>> makegraph(int numCourses, vector<pair<int, int>>& prerequisites){ vector<unordered_set<int>> graph(numCourses); for(auto pre:prerequisites) graph[pre.second].insert(pre.first); return graph; } vector<int> deprees(vector<unordered_set<int>>& graph){ vector<int> indeprees(graph.size(),0); for(auto g:graph) for(int a:g) indeprees[a]++; return indeprees; } vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) { vector<int> res; vector<unordered_set<int>> graph=makegraph(numCourses,prerequisites); vector<int> indeprees=deprees(graph); int num=0; for(int i=0;i<indeprees.size();){ if(!indeprees[i]){ indeprees[i]=-1; res.push_back(i); num++; if(num==indeprees.size()) return res; for(int next:graph[i]) indeprees[next]--; i=0; } else i++; } return {}; } };
以上是关于leetcode 210. Course Schedule II的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode] 210. Course Schedule II
leetcode [210]Course Schedule II
leetcode 210. Course Schedule II
[LeetCode] 210. Course Schedule II Java