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对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
随机推荐
- Shell脚本——for,while,until循环
1.for循环: 语句格式: for i in 循环判断 do 循环体 done 举例:九九乘法表(for循环版本) #!/bin/bash # Author: Sean Martin # Blog: ...
- linux命令详解——xargs
1. 简介 之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了xargs命令,例如: find /sbin -perm +700 |ls -l ...
- How to mount remote Windows shares
Contents Required packages Basic method Better Method Even-better method Yet Another Even-better m ...
- 2.java多线程_synchronized(Lock)同步
1.synchronized同步关键词 线程安全是并发编程中的重要关注点,应该注意到的是,造成线程安全问题的主要诱因有两点,一是存在共享数据(也称临界资源),二是存在多条线程共同 操作共享数据.因此为 ...
- LeetCode03 - 无重复字符的最长子串(Java 实现)
LeetCode03 - 无重复字符的最长子串(Java 实现) 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/longest-substri ...
- java-udp编程
TCP/IP UDP都是基于传输层的:而udp发送数据会出现丢包的情况,发送一个数据不管对方接收不接收,发送过去就完事了: udp的特点:将数据源和目的封装成数据包中,不要建立连接:(Datagram ...
- innerHTML和innerText的使用和区别
document对象中有innerHTML.innerText这两个属性,都是获取document对象文本内容,但使用起来还是有区别的: 1) innerHTML设置或获取标签所包含的HTML+文本信 ...
- java中int转成String位数不足前面补零
java中int转成String位数不足前面补零 转载自:http://ych0108.iteye.com/blog/2174134 java中int转String位数不够前面补零 String.fo ...
- Promise中有多个resove
return new Promise((resolve, reject) => { resolve({ status: }) if (true) { resolve({ status: }) } ...
- Acwing-168-生日蛋糕(搜索, 剪枝)
链接: https://www.acwing.com/problem/content/170/ 题意: 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆 ...