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 ...
随机推荐
- 玩转Libmodbus(二) 写代码体验
libmodbus在线文档 https://www.jianshu.com/p/d93c17485c0a 原创篇 参考上一篇转载的博客,我的ubuntu上的最终生成的动态库的路径,下图所示 我的lin ...
- LeetCode刷题总结-数学篇
本文总结LeetCode上有数学类的算法题,推荐刷题总数为40道.具体考点分析如下图: 1.基本运算问题 题号:29. 两数相除,难度中等 题号:166. 分数到小数,难度中等 题号:372. 超级次 ...
- c++ 中. 和 ->,波浪号 ~ 符号怎么用 ————很重要
参考:https://www.cnblogs.com/Simulation-Campus/p/8809999.html 1. 用在类中的析构函数之前,表示该函数是析构函数.如类A的析构函数 clas ...
- 利用rtklib处理GPS以及北斗数据详解
利用rtklib开源代码处理GPS以及北斗数据详解 在GNSS领域最基础的工作是这些GNSS系统的定位工作,对于绝大多数研究者,自己着手完成这些工作是一个"鸡肋":完全独立设计的话 ...
- aptitude命令
命令 作用 aptitude update 更新可用的包列表 aptitude upgrade 升级可用的包 aptitude dist-upgrade 将系统升级到新的发行版 aptitude in ...
- 远程触发Jenkins的Pipeline任务
场景 虽然能配置提交代码时触发Jenkins任务,但有时并不需要每次提交代码都触发,而是仅在有需要时才执行. 除了在Jenkins页面上手动执行任务,还可以向Jenkins网站发起HTTP请求,触发指 ...
- Hello World背后的事情
Hello World是不少人学习C++的第一项内容,代码看似简单,很多东西却涉及根本 #include <iostream> using namespace std; int main( ...
- intelliJ 软件项目打开运行
1.导入项目 2.首先更改数据库,找到application-dev.yml文件,更改数据源 3.配置tomcat端口 找到application.yml 文件 然后打开pom.xml 更改版本号 ...
- 【C语言/C++编程学习笔记】:通俗易懂讲解 - 链表!学不会?不存在的!
C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...
- firstBlog
第一篇博客 markdown语法的学习 基本语法 标题与段落 在文字前面加1-6个#号来表示来代表一级标题到六级标题 一级标题 五级标题 列表 对于无序列表,可以使用字符-,+和*作为列表的标记 - ...