当前位置:网站首页>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();
}
};
边栏推荐
- Applied stochastic process 01: basic concepts of stochastic process
- Zhongqing reading news
- Raspberry pie serial port login and SSH login methods
- pymongo获取一列数据
- Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
- Establishment and operation of cloud platform open source project environment
- leetcode59. 螺旋矩阵 II(中等)
- Database basics exercise part 2
- 顶测分享:想转行,这些问题一定要考虑清楚!
- Short video, more and more boring?
猜你喜欢
巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
Fast target recognition based on pytorch and fast RCNN
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
首发织梦百度推送插件全自动收录优化seo收录模块
配置树莓派接入网络
作者已死?AI正用藝術征服人類
前缀和数组系列
What is the biggest problem that fresh e-commerce is difficult to do now
3. Business and load balancing of high architecture
随机推荐
What is the difference between int (1) and int (10)? Senior developers can't tell!
树莓派串口登录与SSH登录方法
【刷题】怎么样才能正确的迎接面试?
医疗软件检测机构怎么找,一航软件测评是专家
leetcode841. 钥匙和房间(中等)
[advanced software testing step 1] basic knowledge of automated testing
攻防世界 MISC中reverseMe简述
RichView TRVStyle 模板样式的设置与使用
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
升级版手机检测微信工具小程序源码-支持多种流量主模式
Arduino tutorial - Simon games
PCL实现选框裁剪点云
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
UniPro甘特图“初体验”:关注细节背后的多场景探索
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
漏了监控:Zabbix对Eureka instance状态监控
Applied stochastic process 01: basic concepts of stochastic process
Raspberry pie serial port login and SSH login methods
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
Every API has its foundation when a building rises from the ground