当前位置:网站首页>210. Schedule II - depth traversal
210. Schedule II - depth traversal
2022-06-30 01:41:00 【The_ Dan】
class Solution {
public:
// It's the curriculum 207 An upgraded version of the question , The difference is that the results need to be output in an array
// Different places are marked , For those who don't understand the idea, it is suggested to turn to page 207 Notes to questions
vector<vector<int>> vecVecInt;
vector<int> visited;
bool flag = true;
vector<int> ans;
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
visited.resize(numCourses);
vecVecInt.resize(numCourses);
for(auto i : prerequisites)
vecVecInt[i[0]].push_back(i[1]);
for(int i = 0; i < numCourses && flag; ++i){
if(!visited[i]) // You have to add , Otherwise, the output will be repeated , And can improve efficiency
dfs(i);
}
if(flag) // If you can finish all the courses , Output the answer
return ans;
else // Otherwise, return an empty array
return {
};
}
void dfs(int n){
visited[n] = 1;
int size = vecVecInt[n].size();
for(int i = 0; i < size; ++i){
if(flag && visited[vecVecInt[n][i]] == 0){
dfs(vecVecInt[n][i]);
if(!flag)
return;
}
else if(visited[vecVecInt[n][i]] == 1){
flag = false;
return;
}
}
visited[n] = 2;
ans.push_back(n); // Depth traversal , So just put it at the end
}
};
Accepted
44/44 cases passed (24 ms)
Your runtime beats 31.18 % of cpp submissions
Your memory usage beats 20.07 % of cpp submissions (14.2 MB)
边栏推荐
猜你喜欢
随机推荐
How to seamlessly transition from traditional microservice framework to service grid ASM
Cookie encryption 12
[535. encryption and decryption of tinyurl]
Seata and the three platforms are working together in the summer of programming. Millions of bonuses are waiting for you
【二叉树】最大二叉树 II
C语言 数组元素循环右移问题
Unity2D--给动画添加关键帧并绑定事件
The 8th "Internet +" competition - cloud native track invites you to challenge
cookie加密9
Cookie加密10
GeoTools:WKT、GeoJson、Feature、FeatureCollection相互转换常用工具
Understand AQS principle (flow chart and synchronous queue diagram)
Pytoch modifies the hook source code to obtain per layer output parameters (with layer name)
Can mango hypermedia, which "braves the wind and waves", go ashore?
关于c语言main函数中int argc,char **argv的理解
Internal class usage scenarios, syntax and principle explanations
js逆向请求参数加密:
[machine learning Q & A] accuracy, accuracy, recall, ROC and AUC
Pytorch模型训练到哪里找预训练模型?
C language score ranking








