当前位置:网站首页>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();
}
};
边栏推荐
- Interface automation test framework: pytest+allure+excel
- Huawei equipment configuration ospf-bgp linkage
- kubernetes集群搭建Zabbix监控平台
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- 3. Business and load balancing of high architecture
- ROS learning_ Basics
- 【刷题】怎么样才能正确的迎接面试?
- 攻防世界 MISC中reverseMe简述
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- “无聊猿” BAYC 的内忧与外患
猜你喜欢

【每日一题】729. 我的日程安排表 I

指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品

Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-

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

Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
![[daily question] 729 My schedule I](/img/6b/a9fef338ac09caafe628023f066e1f.png)
[daily question] 729 My schedule I

leetcode35. 搜索插入位置(简单,找插入位置,不同写法)

接口自动化测试框架:Pytest+Allure+Excel

After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.

18. Multi level page table and fast table
随机推荐
[some special grammars about C]
Depth residual network
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
[hot100] 739. Température quotidienne
Three methods of adding color to latex text
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
软件测试外包到底要不要去?三年真实外包感受告诉你
【刷题】怎么样才能正确的迎接面试?
Visitor tweets about how you can layout the metauniverse
WPF之MVVM
18.多级页表与快表
Proteus -- Serial Communication parity flag mode
1189. Maximum number of "balloons"
升级版手机检测微信工具小程序源码-支持多种流量主模式
leetcode59. 螺旋矩阵 II(中等)
Zhongqing reading news
攻防世界 MISC中reverseMe简述
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
LeetCode Algorithm 2181. 合并零之间的节点