首页 > 代码库 > leetcode 210. Course Schedule II
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。