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

原文地址:https://www.cnblogs.com/jinjin-2018/p/9098140.html

时间: 2024-08-30 11:42:50

841. Keys and Rooms —— weekly contest 86的相关文章

Leetcode Weekly Contest 86

Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 "幻方" 子矩阵?(每个子矩阵都是连续的). 直接模拟即可,本来是签到题,由于粗心,浪费了时间. 1 class Solution { 2 public: 3 int numMagicSquaresInside(vector&l

840. Magic Squares In Grid ——weekly contest 86

题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time:5ms 本人的解法过于粗糙,看出了中间必须是5,然后比较每行每列每对角线的值是否相等. class Solution { public: int numMagicSquaresInside(vector<vector<int>>& grid) { int n = grid.si

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

题目链接:https://leetcode.com/problems/split-array-into-fibonacci-sequence/description/ 占坑. string 的数值转换函数(c++11)介绍 :https://blog.csdn.net/calmreason/article/details/41204211 答案可参考:https://leetcode.com/problems/split-array-into-fibonacci-sequence/discuss

843. Guess the Word —— weekly contest 86

题目链接:https://leetcode.com/problems/guess-the-word/description/ 占坑 据说要用启发式算法,可参考下述答案进行学习:https://leetcode.com/problems/guess-the-word/discuss/133862/Random-Guess-and-Minimax-Guess-with-Comparison 原文地址:https://www.cnblogs.com/jinjin-2018/p/9098158.html

LeetCode 841:钥匙和房间 Keys and Rooms

题目: ? 有 N 个房间,开始时你位于 0 号房间.每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间. ? 在形式上,对于每个房间 i 都有一个钥匙列表 rooms[i],每个钥匙 rooms[i][j] 由 [0,1,...,N-1] 中的一个整数表示,其中 N = rooms.length. 钥匙 rooms[i][j] = v 可以打开编号为 v 的房间. 最初,除 0 号房间外的其余所有房间都被锁住. 你可以自由地在房间之间来回走动. 如果

LeetCode之Weekly Contest 93

第一题:二进制间距 问题: 给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的二进制是 0b10110 . 在 22 的二进制表示中,有三个 1,组成两对连续的 1 . 第一对连续的 1 中,两个 1 之间的距离为 2 . 第二对连续的 1 中,两个 1 之间的距离为 1 . 答案取两个距离之中最大的,也就是 2 . 示例 2: 输入:5 输出:2 解释: 5 的二进制是 0

leetcode841 Keys and Rooms

1 """ 2 There are N rooms and you start in room 0. Each room has a distinct number in 0, 1, 2, ..., N-1, and each room may have some keys to access the next room. 3 Formally, each room i has a list of keys rooms[i], and each key rooms[i][j]

LeetCode之Weekly Contest 101

前一段时间比较忙,而且做这个对于我来说挺耗时间的,已经间隔了几期的没做总结了,后面有机会补齐.而且本来做这个的目的就是为了防止长时间不做把编程拉下,不在追求独立作出所有题了.以后完赛后稍微尝试下,做不出来的直接放弃. 第一题:问题 问题:900. RLE 迭代器 编写一个遍历游程编码序列的迭代器. 迭代器由 RLEIterator(int[] A) 初始化,其中 A 是某个序列的游程编码.更具体地,对于所有偶数i,A[i] 告诉我们在序列中重复非负整数值 A[i + 1] 的次数. 迭代器支持一

[Swift Weekly Contest 109]LeetCode934. 最短的桥 | Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two islands together to form 1 island. Return the smallest nu