Re: [閒聊] 每日leetcode
802.
拓撲拉機獸
嗎?
class Solution {
public:
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
int n = graph.size();
vector<vector<int>> adj(n);
vector<int> out(n);
for(int i = 0; i < n; i++){
for(auto& node: graph[i]){
adj[node].push_back(i);
out[i]++;
}
}
vector<int> res;
queue<int> ter;
for(int i = 0; i < n; i++){
if(out[i] == 0){
ter.push(i);
res.push_back(i);
}
}
while(!ter.empty()){
int p = ter.front();
ter.pop();
for(auto& i: adj[p]){
if(--out[i] == 0){
ter.push(i);
res.push_back(i);
}
}
}
ranges::sort(res);
return res;
}
};
找cycle感覺也可以 不過寫完ㄌ就這樣ㄅ
怎麼剛回來就沒人刷題ㄌ==
過年放假ㄌ484
--
很姆的咪
姆之咪
http://i.imgur.com/5sw7QOj.jpg

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.205.121.194 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1737727686.A.C08.html
討論串 (同標題文章)
完整討論串 (本文為第 1303 之 1553 篇):