当前位置:网站首页>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();
}
};
边栏推荐
- 作者已死?AI正用藝術征服人類
- 【每日一题】729. 我的日程安排表 I
- Fedora/REHL 安装 semanage
- Fedora/rehl installation semanage
- 指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
- The registration password of day 239/300 is 8~14 alphanumeric and punctuation, and at least 2 checks are included
- 【Hot100】739. Daily temperature
- What is the difference between int (1) and int (10)? Senior developers can't tell!
- Refer to how customer push e-commerce does content operation
- 19.段页结合的实际内存管理
猜你喜欢

Market segmentation of supermarket customers based on purchase behavior data (RFM model)

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

How to find a medical software testing institution? First flight software evaluation is an expert

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

MPLS experiment

Interface automation test framework: pytest+allure+excel

这个高颜值的开源第三方网易云音乐播放器你值得拥有

leetcode1020. 飞地的数量(中等)

Proteus -- Serial Communication parity flag mode

接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
随机推荐
leetcode704. 二分查找(查找某个元素,简单,不同写法)
Proteus -- Serial Communication parity flag mode
Compile, connect -- notes-2
Leetcode daily question (1870. minimum speed to arrive on time)
UniPro甘特图“初体验”:关注细节背后的多场景探索
巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
Thought map of data warehouse construction
SAP SD发货流程中托盘的管理
Call, apply, bind rewrite, easy to understand with comments
Simple use of MySQL database: add, delete, modify and query
Reflex WMS medium level series 3: display shipped replaceable groups
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
UDP攻击是什么意思?UDP攻击防范措施
【软件测试进阶第1步】自动化测试基础知识
Cif10 actual combat (resnet18)
Huawei equipment configuration ospf-bgp linkage
TS Basics
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
SEO学习的最好方式:搜索引擎
Top test sharing: if you want to change careers, you must consider these issues clearly!