题目链接: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的更多相关文章

  1. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  2. 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 ...

  3. LeetCode 841. Keys and Rooms

    原题链接在这里:https://leetcode.com/problems/keys-and-rooms/ 题目: There are N rooms and you start in room 0. ...

  4. 【LeetCode】841. Keys and Rooms 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  5. 843. Guess the Word —— weekly contest 86

    题目链接:https://leetcode.com/problems/guess-the-word/description/ 占坑 据说要用启发式算法,可参考下述答案进行学习:https://leet ...

  6. 842. Split Array into Fibonacci Sequence —— weekly contest 86

    题目链接:https://leetcode.com/problems/split-array-into-fibonacci-sequence/description/ 占坑. string 的数值转换 ...

  7. 840. Magic Squares In Grid ——weekly contest 86

    题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time: ...

  8. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  9. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

随机推荐

  1. 阿里云oss对象存储配置CDN

    阿里云oss对象存储配置CDN 1.打开阿里云CDN 2.填写信息,这个地方要注意,我的备案域名是www.ljwXXX.work,我们可以自定义一个域名,test.ljwXXX.work作为加速域名. ...

  2. 【题解】NOIP2018 赛道修建

    题目戳我 \(\text{Solution:}\) 根据题目信息简化题意,是让你在树上找出\(m\)条路径使得路径长度最小值最大. 看到题第一感先二分一个答案,问题转化为如何选择一些路径使得它们最小值 ...

  3. 【学习笔记】Polya定理

    笔者经多番周折终于看懂了\(\text{Burnside}\)定理和\(\text{Polya}\)定理,特来写一篇学习笔记来记录一下. 群定义 定义:群\((G,·)\)是一个集合与一个运算·所定义 ...

  4. Vue自定义Popup弹窗组件|vue仿ios、微信弹窗|vue右键弹层

    基于vue.js构建的轻量级Vue移动端弹出框组件Vpopup vpopup 汇聚了有赞Vant.京东NutUI等Vue组件库的Msg消息框.Popup弹层.Dialog对话框.Toast弱提示.Ac ...

  5. PHP的学习(提前学习了,业余爱好) (一)

    一个函数一个函数地堆 strstr()函数 在本地测试的时候,代码与显示如下 1.代码: <?php echo strstr("I love Shanghai!123",&q ...

  6. 题解:POI2012 Salaries

    题解:POI2012 Salaries Description The Byteotian Software Corporation (BSC) has \(n\) employees. In BSC ...

  7. 【KM算法】UVA 11383 Golden Tiger Claw

    题目大意 给你一个\(n×n\)的矩阵G,每个位置有一个权,求两个一维数组\(row\)和\(col\),使\(row[i] + col[j]\ge G[i][j]\),并且\(∑row+∑col\) ...

  8. php进程 swoole

    <?php $pid = posix_getpid(); $ppid = posix_getppid(); var_dump($pid); cli_set_process_title(" ...

  9. lumen路由

    $router->get('/', function () use ($router) { return config('options.author'); }); $router->ge ...

  10. scrapy Request方法

    # -*- coding: utf-8 -*- import scrapy class TestSpider(scrapy.Spider): name = 'test' allowed_domains ...