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, and each room may have some keys to access the next room.
Formally, each room i has a list of keys rooms[i], and each key rooms[i][j] is an integer in [0, 1, ..., N-1] where N = rooms.length. A key rooms[i][j] = v opens the room with number v.
Initially, all the rooms start locked (except for room 0).
You can walk back and forth between rooms freely.
Return true if and only if you can enter every room.
Example 1:
Input: [[1],[2],[3],[]]
Output: true
Explanation:
We start in room 0, and pick up key 1.
We then go to room 1, and pick up key 2.
We then go to room 2, and pick up key 3.
We then go to room 3. Since we were able to go to every room, we return true.
Example 2:
Input: [[1,3],[3,0,1],[2],[0]]
Output: false
Explanation: We can't enter the room with number 2.
Note:
1 <= rooms.length <= 10000 <= rooms[i].length <= 1000- The number of keys in all rooms combined is at most
3000.
Runtime: 8 ms, faster than 63.18% of C++ online submissions for Keys and Rooms.
class Solution {
public:
bool canVisitAllRooms(vector<vector<int>>& rooms) {
vector<bool> visited(rooms.size(),false);
visited[] = true;
queue<int> q;
q.push();
while(!q.empty()){
int idx = q.front(); q.pop();
for(auto x : rooms[idx]){
if(!visited[x]) {
visited[x] = true;
q.push(x);
}
}
}
for(auto x : visited) if(!x) return false;
return true;
}
};
LC 841. Keys and Rooms的更多相关文章
- 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 ...
- 841. Keys and Rooms —— weekly contest 86
题目链接:https://leetcode.com/problems/keys-and-rooms/description/ 简单DFS time:9ms 1 class Solution { 2 p ...
- LeetCode 841:钥匙和房间 Keys and Rooms
题目: 有 N 个房间,开始时你位于 0 号房间.每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间. 在形式上,对于每个房间 i 都有一个钥匙列表 ...
- [LeetCode] 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 ...
- [Swift]LeetCode841. 钥匙和房间 | 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 ...
- leetcode841 Keys and Rooms
""" There are N rooms and you start in room 0. Each room has a distinct number in 0, ...
- LeetCode题解之Keys and Rooms
1.题目描述 2.问题分析 使用深度优先遍历 3.代码 bool canVisitAllRooms(vector<vector<int>>& rooms) { int ...
- 算法与数据结构基础 - 深度优先搜索(DFS)
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
随机推荐
- 使用tinyproxy进行ip代理
爬虫经常用到ip代理.解决方案无非几种: 1.网络上寻找一些免费代理,优点:免费不限量:缺点:可用性较低,验证费时间费资源.一些有免费代理的网站,西刺代理,站大爷,89免费代理等等,网上可以搜出一大堆 ...
- Toast的基本用法 吐司打印
//Toast.makeText(上下文,内容,显示时间);Toast toast =Toast.makeText(this,"位置="+position+"内容=&qu ...
- android如何自动切换后台app
在做 android 前端性能自动化测试时,你是否经历过这样的场景:依次启动 A.B.C 三个 app,其中 C 是被测 app, 当当前屏幕切换到 B 进行相关操作后,如何通过封装方法实现自动切换会 ...
- 单调队列优化&&P1886 滑动窗口题解
单调队列: 顾名思义,就是队列中元素是单调的(单增或者单减). 在某些问题中能够优化复杂度. 在dp问题中,有一个专题动态规划的单调队列优化,以后会更新(现在还是太菜了不会). 在你看到类似于滑动定长 ...
- Django :执行 python manage.py makemigrations 时报错 TypeError: __init__() missing 1 required positional argument: 'on_delete'
原因 执行命令 python manage.py makemigrations 报错 TypeError: __init__() missing 1 required positional argum ...
- Typescript玩转设计模式 之 创建型模式
作者简介 joey 蚂蚁金服·数据体验技术团队 前言 我们团队的工作是用单页面应用的方式实现web工具.涉及到数万到十数万行的前端代码的管理,而且项目周期长达数年. 怎么样很好地管理好这种量级的前端代 ...
- 201871010126 王亚涛 《面向对象程序设计 (Java)》第十七周学习总结
内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p/12 ...
- PyQt打包可执行文件
1.安装pyinstaller pip install pyinstaller 2.pyinstaller打包 pyinstaller -F -w xxxx.py -F:生成可执行文件 -w:不显示命 ...
- 设计一个Mypoint类,求两个点之间的距离
package Test; public class test6 { public static void main(String[] args) { // TODO Auto-generated m ...
- hihocoder 1457 后缀自动机四·重复旋律7 ( 多串连接处理技巧 )
题目链接 分析 : 这道题对于单个串的用 SAM 然后想想怎么维护就行了 但是多个串下.可以先将所有的串用一个不在字符集( 这道题的字符集是 '0' ~ '9' ) 链接起来.建立后缀自动机之后 在统 ...