841. Keys and Rooms —— weekly contest 86
题目链接:https://leetcode.com/problems/keys-and-rooms/description/
简单DFS
time:9ms
1 class Solution {
2 public:
3 void DFS(int root,vector<int>& visited,vector<vector<int>>& rooms){
4 visited[root] = 1;
5 for(auto x : rooms[root]){
6 if(visited[x] == 0){
7 DFS(x,visited,rooms);
8 }
9 }
10 }
11 bool canVisitAllRooms(vector<vector<int>>& rooms) {
12 vector<int> visited;
13 int n = rooms.size();
14 visited.assign(n,0);
15 DFS(0,visited,rooms);
16 for(int i = 0; i < n; i++){
17 if(visited[i] == 0){
18 return false;
19 }
20 }
21 return true;
22 }
23
24 };
看到别人的用堆栈实现的dfs也贴一下
1 bool canVisitAllRooms(vector<vector<int>>& rooms) {
2 stack<int> dfs; dfs.push(0);
3 unordered_set<int> seen = {0};
4 while (!dfs.empty()) {
5 int i = dfs.top(); dfs.pop();
6 for (int j : rooms[i])
7 if (seen.count(j) == 0) {
8 dfs.push(j);
9 seen.insert(j);
10 if (rooms.size() == seen.size()) return true;
11 }
12 }
13 return rooms.size() == seen.size();
14 }
出处:https://leetcode.com/problems/keys-and-rooms/discuss/133855/Straight-Forward
841. Keys and Rooms —— weekly contest 86的更多相关文章
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LC 841. Keys and Rooms
There are N rooms and you start in room 0. Each room has a distinct number in 0, 1, 2, ..., N-1, an ...
- LeetCode 841. Keys and Rooms
原题链接在这里:https://leetcode.com/problems/keys-and-rooms/ 题目: There are N rooms and you start in room 0. ...
- 【LeetCode】841. Keys and Rooms 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 843. Guess the Word —— weekly contest 86
题目链接:https://leetcode.com/problems/guess-the-word/description/ 占坑 据说要用启发式算法,可参考下述答案进行学习:https://leet ...
- 842. Split Array into Fibonacci Sequence —— weekly contest 86
题目链接:https://leetcode.com/problems/split-array-into-fibonacci-sequence/description/ 占坑. string 的数值转换 ...
- 840. Magic Squares In Grid ——weekly contest 86
题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time: ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
随机推荐
- 商品现货数据不好拿?商品季节性难跟踪?一键解决没烦恼的Python爬虫分享
更多精彩内容,欢迎关注公众号:数量技术宅.探讨数据分析.量化投资问题,请加技术宅微信:sljsz01 季节性在大宗商品的交易中至关重要 大宗商品交易中,特别是在期货市场,由于商品价格周期的存在,季节性 ...
- 01 AS 首次编译执行项目过程中遇到的几个常见问题
问题01 as打开时出现The environment variable JAVA_HOME (with The value of C:\Java\jdk1.8.0_101\bin) does not ...
- JavaFX ComboBox的选中事项
参考1:https://blog.csdn.net/mexel310/article/details/37909205 参考2:https://blog.csdn.net/maosijunzi/art ...
- Varnish 6.2.2 的介绍与安装
一.简介 Varnish 是一款高性能且开源的反向代理服务器和 HTTP 加速器,其采用全新的软件体系机构,和现在的硬件体系紧密配合,与传统的 Squid 相比,Varnish 具有性能更高.速度更快 ...
- 多测师讲解RF自动化测试实现流程_高级讲师肖sir
1.环境搭建过程?整套环境需要哪些工具包,以及工具包的作用?因为我搭建的RF框架是基于Python的,所以肯定要先安装Python,python安装完之后,开始安装自动化测试框架rf3.0-在do ...
- day07 Pyhton学习
一.昨日内容回顾 小数据池,常量池 id()内存地址 is == 的区别 is 判断的是内存地址 == 判断的是值 存在的意义: 快速的创建字符串,整数,布尔值的对象 帮你节省内存 解码和编码 enc ...
- Jmeter请求之接口串联自动化测试(未完)
方案一:添加Cookie管理器,把用户的登录状态存在cookie管理器中,类似于浏览器 存储测试结果: 监听器->保存响应到文件,对结果进行存储 文件名前缀:保存到哪个地方前缀是什么D:\tes ...
- 【Windows编程】入门篇——win 32窗口的hello word!
✍ Windows编程基础 1.Win 32应用程序基本类型 1) 控制台程序 不需要完善的windows窗口,可以使用DOS窗口方式显示 2) Win 32窗口程序 包含窗口的程序,可以通过窗 ...
- 【折半枚举+二分】POJ 3977 Subset
题目内容 Vjudge链接 给你\(n\)个数,求出这\(n\)个数的一个非空子集,使子集中的数加和的绝对值最小,在此基础上子集中元素的个数应最小. 输入格式 输入含多组数据,每组数据有两行,第一行是 ...
- linux(centos8):用systemctl管理war包形式的jenkins(java 14 / jenkins 2.257)
一,如何安装jenkins? 参见: https://www.cnblogs.com/architectforest/p/13685904.html 说明:刘宏缔的架构森林是一个专注架构的博客,地址: ...