当前位置:网站首页>leetcode841. Keys and rooms (medium)
leetcode841. Keys and rooms (medium)
2022-07-06 07:03:00 【Heavy garbage】
Template questions for search
Train of thought :dfs
class Solution {
public:
int flag[1005] = {
0};
int dfs(vector<vector<int>>& rooms, int idx) {
int cnt = 1;
flag[idx] = 1;
for (int i = 0; i < rooms[idx].size(); ++i) {
if (!flag[rooms[idx][i]]) cnt += dfs(rooms,rooms[idx][i]);
}
return cnt;
}
bool canVisitAllRooms(vector<vector<int>>& rooms) {
return dfs(rooms, 0) == rooms.size();
}
};
Train of thought two :bfs
class Solution {
public:
int flag[1005] = {
0};
bool canVisitAllRooms(vector<vector<int>>& rooms) {
queue<int> q;
q.push(0);
flag[0] = 1;
int cnt = 0;
while (!q.empty()) {
int frt = q.front();
q.pop();
cnt++;
for (int i = 0; i < rooms[frt].size(); ++i) {
if (!flag[rooms[frt][i]]) {
q.push(rooms[frt][i]);
flag[rooms[frt][i]] = 1;
}
}
}
return cnt == rooms.size();
}
};
边栏推荐
- “无聊猿” BAYC 的内忧与外患
- Introduction to ros2 installation and basic knowledge
- 《从0到1:CTFer成长之路》书籍配套题目(周更)
- Reflex WMS中阶系列3:显示已发货可换组
- Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
- Misc of BUU (update from time to time)
- Chapter 7 - thread pool of shared model
- How to reconstruct the class explosion caused by m*n strategies?
- 指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
- 机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
猜你喜欢
Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
Short video, more and more boring?
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
leetcode841. 钥匙和房间(中等)
hydra常用命令
树莓派串口登录与SSH登录方法
Missing monitoring: ZABBIX monitors the status of Eureka instance
What is the biggest problem that fresh e-commerce is difficult to do now
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
随机推荐
呆错图床系统源码图片CDN加速与破解防盗链功能
BIO模型实现多人聊天
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
WPF之MVVM
18. Multi level page table and fast table
Establishment and operation of cloud platform open source project environment
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
攻防世界 MISC中reverseMe简述
【Hot100】739. 每日温度
Compile, connect -- notes-2
Top test sharing: if you want to change careers, you must consider these issues clearly!
Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
What does UDP attack mean? UDP attack prevention measures
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
Latex文字加颜色的三种办法
树莓派3B更新vim
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
这个高颜值的开源第三方网易云音乐播放器你值得拥有
[daily question] 729 My schedule I
Visitor tweets about how you can layout the metauniverse