当前位置:网站首页>leetcode841. 钥匙和房间(中等)
leetcode841. 钥匙和房间(中等)
2022-07-06 06:55:00 【重you小垃】



搜索的模板题
思路一: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();
}
};
思路二: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();
}
};
边栏推荐
- Huawei equipment configuration ospf-bgp linkage
- A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
- Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
- Day 248/300 thoughts on how graduates find jobs
- ROS2安装及基础知识介绍
- Every API has its foundation when a building rises from the ground
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- Hydra common commands
- Three methods of adding color to latex text
- Market segmentation of supermarket customers based on purchase behavior data (RFM model)
猜你喜欢

Windows Server 2016 standard installing Oracle

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Database basics exercise part 2

AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm

详解SQL中Groupings Sets 语句的功能和底层实现逻辑

leetcode704. 二分查找(查找某个元素,简单,不同写法)

同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励

Visitor tweets about how you can layout the metauniverse

Hydra common commands

接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
随机推荐
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
18.多级页表与快表
Leetcode - 152 product maximum subarray
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Basic commands of MySQL
软件测试外包到底要不要去?三年真实外包感受告诉你
Day 248/300 thoughts on how graduates find jobs
[daily question] 729 My schedule I
Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中
医疗软件检测机构怎么找,一航软件测评是专家
ROS学习_基础
GET 和 POST 请求类型的区别
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
【Hot100】739. Daily temperature
PCL realizes frame selection and clipping point cloud
Refer to how customer push e-commerce does content operation
Database basics exercise part 2
【软件测试进阶第1步】自动化测试基础知识
SSO process analysis