标签:front site course rap color return ++ -- lse
1 class Solution 2 { 3 vector<int> res; 4 public: 5 vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) 6 { 7 vector<vector<int>> graph(numCourses); 8 vector<int> in_degree(numCourses, 0); 9 for (int i = 0; i < prerequisites.size(); i++) 10 { 11 in_degree[prerequisites[i][0]]++; 12 graph[prerequisites[i][1]].push_back(prerequisites[i][0]); 13 } 14 15 queue<int> q; 16 vector<bool> vis(numCourses, false); 17 18 for (int i = 0; i < numCourses; i++) 19 if (in_degree[i] == 0) 20 q.push(i); 21 while (!q.empty()) 22 { 23 int sta = q.front(); 24 q.pop(); 25 vis[sta] = true; 26 res.push_back(sta); 27 28 //有哪些邻边 29 for (int i = 0; i < graph[sta].size(); i++) 30 { 31 in_degree[graph[sta][i]]--;// 入度-1 32 if (in_degree[graph[sta][i]] == 0) //入度如果为0,加入队列 33 q.push(graph[sta][i]); 34 } 35 } 36 37 //0->1->2 38 for (int i = 0; i < numCourses; i++) 39 if (vis[i] == false)//只要有false,则返回{} 40 return {};//有环 41 return res; 42 } 43 };
标签:front site course rap color return ++ -- lse
原文地址:https://www.cnblogs.com/yuhong1103/p/12633615.html