当前位置:网站首页>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();
}
};
边栏推荐
- The difference between get and post request types
- Windows Server 2016 standard installing Oracle
- Every API has its foundation when a building rises from the ground
- Prefix and array series
- Top test sharing: if you want to change careers, you must consider these issues clearly!
- Proteus -- Serial Communication parity flag mode
- Reflex WMS中阶系列3:显示已发货可换组
- 万丈高楼平地起,每个API皆根基
- “无聊猿” BAYC 的内忧与外患
- 树莓派串口登录与SSH登录方法
猜你喜欢

Development of entity developer database application

Fast target recognition based on pytorch and fast RCNN

雲上有AI,讓地球科學研究更省力

BUU的MISC(不定时更新)
![[daily question] 729 My schedule I](/img/6b/a9fef338ac09caafe628023f066e1f.png)
[daily question] 729 My schedule I

【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例

18. Multi level page table and fast table

Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案

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

Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
随机推荐
Fedora/rehl installation semanage
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
编译,连接 -- 笔记 -2
UWA pipeline version 2.2.1 update instructions
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
Proteus -- Serial Communication parity flag mode
ROS学习_基础
3. Business and load balancing of high architecture
[brush questions] how can we correctly meet the interview?
leetcode704. 二分查找(查找某个元素,简单,不同写法)
librosa音频处理教程
顶测分享:想转行,这些问题一定要考虑清楚!
Windows Server 2016 standard installing Oracle
leetcode6109. 知道秘密的人数(中等,周赛)
PCL实现选框裁剪点云
【Hot100】739. Daily temperature
雲上有AI,讓地球科學研究更省力
Entity Developer数据库应用程序的开发
Do you really know the use of idea?
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple