标签:style second return i++ cond push logs ret res
跟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
标签:style second return i++ cond push logs ret res
原文地址:http://www.cnblogs.com/weedboy/p/6818828.html