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 ...
随机推荐
- 如何安装eclipse
1.打开浏览器输入网址:http://www.eclipse.org 进入官方 2.(目前我使用windows操作系统),下拉界面选择"windows"后的"64-bit ...
- Arduino 串行外设接口——W3Cschool
来源:https://www.w3cschool.cn/arduino/arduino_serial_peripheral_interface.html Arduino 串行外设接口 由 drbear ...
- VS Code 搭建编写Shell环境(WSL)
安装过程 Win10开启WSL,方法略 安装VSCode,方法略 安装语法提示插件:shellman 安装格式化插件:shell-format(右键 -> 格式化文档(Ctrl + Alt + ...
- 跟我一起学Redis之五种基本类型及其应用场景举例(干了6个小时)
前言 来啦,老弟?来啦,上一篇就当唠唠嗑,接下来就开始进行实操撸命令,计划是先整体单纯说说Redis的各种用法和应用,最后再结合代码归纳总结. Redis默认有16个数据库(编号为0~15),默认使用 ...
- python简单实现论文查重(软工第一次项目作业)
前言 软件工程 https://edu.cnblogs.com/campus/gdgy/informationsecurity1812 作业要求 https://edu.cnblogs.com/cam ...
- C#数据结构-链栈
上一篇我们通过数组结构实现了栈结构(准确的说是栈的顺序存储结构),现在我们通过链(单链)存储栈,也就是链栈. 通常对于正向单链表来说,是从头节点开始,在链的尾部附加节点,前一个节点的指针指向附加节点: ...
- rs232转网络
rs232转网络 rs232转网络ZLAN5103可以实现RS232/485/422和TCP/IP之间进行透明数据转发.方便地使得串口设备连接到以太网和Internet,实现串口设备的网络化升级.支持 ...
- Verilog基础入门——简单的语句块编写(一)
[题干] [代码] module top_module ( input in, output out ); assign out = ~in; endmodule 简单的实现一个非门
- go 不停模拟 写日志
package main import ( "os" "errors" "math/rand" "time" " ...
- anaconda 取消每次默认启动base环境
安装conda后取消命令行前出现的base,取消每次启动自动激活conda的基础环境 方法一: 每次在命令行通过conda deactivate退出base环境回到系统自动的环境 方法二 1,通过将a ...