当前位置:网站首页>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();
}
};
边栏推荐
- Basic commands of MySQL
- NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
- 从autojs到冰狐智能辅助的心里历程
- leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
- Latex文字加颜色的三种办法
- TS Basics
- leetcode59. 螺旋矩阵 II(中等)
- Fast target recognition based on pytorch and fast RCNN
- Thought map of data warehouse construction
- 开源的网易云音乐API项目都是怎么实现的?
猜你喜欢
Misc of BUU (update from time to time)
L'auteur est mort? Ai utilise l'art pour conquérir l'humanité
idea控制台彩色日志
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
云上有AI,让地球科学研究更省力
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
19. Actual memory management of segment page combination
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
前缀和数组系列
随机推荐
[hot100] 739. Température quotidienne
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
Visitor tweets about how you can layout the metauniverse
LeetCode Algorithm 2181. 合并零之间的节点
Refer to how customer push e-commerce does content operation
Development of entity developer database application
Blue Bridge Cup zero Foundation National Championship - day 20
顶测分享:想转行,这些问题一定要考虑清楚!
Latex文字加颜色的三种办法
Pymongo gets a list of data
OpenGL ES 学习初识(1)
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
[brush questions] how can we correctly meet the interview?
Hydra common commands
Setting and using richview trvstyle template style
漏了监控:Zabbix对Eureka instance状态监控
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
Every API has its foundation when a building rises from the ground
1189. Maximum number of "balloons"
树莓派串口登录与SSH登录方法