当前位置:网站首页>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();
}
};
边栏推荐
- 19. Actual memory management of segment page combination
- The psychological process from autojs to ice fox intelligent assistance
- [hot100] 739. Température quotidienne
- 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
- 数据仓库建设思维导图
- Day 248/300 thoughts on how graduates find jobs
- BIO模型实现多人聊天
- [some special grammars about C]
- Pymongo gets a list of data
- SEO学习的最好方式:搜索引擎
猜你喜欢
LeetCode 78:子集
Database basics exercise part 2
树莓派串口登录与SSH登录方法
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
Reflex WMS中阶系列3:显示已发货可换组
《从0到1:CTFer成长之路》书籍配套题目(周更)
Simple use of MySQL database: add, delete, modify and query
C语言_双创建、前插,尾插,遍历,删除
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
随机推荐
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
ROS2安装及基础知识介绍
OpenGL ES 学习初识(1)
Latex文字加颜色的三种办法
pymongo获取一列数据
RichView TRVStyle 模板样式的设置与使用
将ue4程序嵌入qt界面显示
The registration password of day 239/300 is 8~14 alphanumeric and punctuation, and at least 2 checks are included
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
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
[brush questions] how can we correctly meet the interview?
ROS learning_ Basics
LeetCode Algorithm 2181. 合并零之间的节点
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
Establishment and operation of cloud platform open source project environment
Huawei equipment configuration ospf-bgp linkage
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
中青看点阅读新闻
Compile, connect -- notes-2
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections