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 号房间外的其余所有房间都被锁住。
你可以自由地在房间之间来回走动。
如果能进入每个房间返回 true,否则返回 false。
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.
示例 1:
输入: [[1],[2],[3],[]]
输出: true
解释:
我们从 0 号房间开始,拿到钥匙 1。
之后我们去 1 号房间,拿到钥匙 2。
然后我们去 2 号房间,拿到钥匙 3。
最后我们去了 3 号房间。
由于我们能够进入每个房间,我们返回 true。
示例 2:
输入:[[1,3],[3,0,1],[2],[0]]
输出:false
解释:我们不能进入 2 号房间。
提示:
1 <= rooms.length <= 10000 <= rooms[i].length <= 1000- 所有房间中的钥匙数量总计不超过
3000。
Note:
1 <= rooms.length <= 10000 <= rooms[i].length <= 1000- The number of keys in all rooms combined is at most
3000.
解题思路:
很简单的一道题,从0号房间开始递归遍历就可以了。唯一需要注意的是如何判断房间是否访问过。可以用set哈希表把已访问过的房间号记录下来,最后如果哈希表长度和rooms长度相等,那么就意味着所有房间均可到达。
代码:
Set集合(Java):
class Solution {
Set<Integer> set = new LinkedHashSet<>();
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
helper(rooms, 0);
return set.size() == rooms.size();//长度相等则可以到达所有房间
}
private void helper(List<List<Integer>> rooms, int index) {
if (set.contains(index)) return;
set.add(index);//已访问房间号加入哈希表
for (Integer i : rooms.get(index)) {//遍历房间的每一个钥匙
helper(rooms, i);//进入递归
}
}
}
可以看到用哈希表解题方法在递归期间会多出许多set函数的调用,如 set.add() 、set.contains(),相对很多这道题解题时间,这个开销是很大。对这道题而言,是完全可以用数组避免的。
Java:
class Solution {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
int len = rooms.size();
int[] visited = new int[len];//初始化等长数组,数组每个值默认为0
helper(rooms, 0, visited);
for (int i : visited)
if (i == 0) return false;//数组中依然有0,则证明有房间未访问到
return true;
}
private void helper(List<List<Integer>> rooms, int index, int[] visited) {
if (visited[index] == 1) return;//如果该房间已访问过,直接返回
visited[index] = 1;//在访问过的房间,改为1来标记
for (Integer i : rooms.get(index)) {//遍历
helper(rooms, i, visited);
}
}
}
Python:
class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
size=len(rooms)
self.visited = [False]*size # 初始化等长数组,默认为False,所有房间均未访问
self.helper(rooms, 0)
return all(self.visited)
def helper(self, rooms: List[List[int]], index: int) -> None:
if self.visited[index]:#如果为True,该房间已访问过,直接返回
return
self.visited[index] = True #在访问的房间标记为True
for i in rooms[index]:#遍历钥匙
self.helper(rooms, i)#进入递归函数
欢迎关注微.信公.众号:爱写Bug

LeetCode 841:钥匙和房间 Keys and Rooms的更多相关文章
- [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 ...
- 【LeetCode】841. 钥匙和房间
841. 钥匙和房间 知识点:图:递归 题目描述 有 N 个房间,开始时你位于 0 号房间.每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间. 在形式上, ...
- Leetcode-841. 钥匙和房间
题目 有 N 个房间,开始时你位于 0 号房间.每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间. 在形式上,对于每个房间 i 都有一个钥匙列表 room ...
- 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 ...
- 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 钥匙与房间
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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- LeetCode 841. Keys and Rooms
原题链接在这里:https://leetcode.com/problems/keys-and-rooms/ 题目: There are N rooms and you start in room 0. ...
- 841. Keys and Rooms —— weekly contest 86
题目链接:https://leetcode.com/problems/keys-and-rooms/description/ 简单DFS time:9ms 1 class Solution { 2 p ...
随机推荐
- oidc hybrid flow 与另外两种模式的异同
很多学习identityserver的文章都没有解释清楚oidc hybrid混合模式的含义.本文将解释hybrid模式与另外两种模式的主要区别. 我们先看一下一手文档: https://openid ...
- 玩下PHP的分词,最近有这个需求
找了个地方 下载代码 我是在这里下载的 https://www.jb51.net/codes/65593.html 1 下载完毕后 打开是这样的文件 2 先把代码集成到thinkphp3.2.3里 ...
- WPF图片,DataGrid等实现圆角
<Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Grid.Ro ...
- 帝国CMS标签【操作类型】说明详解
看标签的参数时候,一般最后一个参数是操作类型说明,可是后面写的是:"操作类型说明 具体看操作类型说明", 这个操作类型说明在什么地方看啊 操作类型 说明 操作类型 说明 0 各栏目 ...
- tinyriscv---一个从零开始写的极简、易懂的开源RISC-V处理器核
本项目实现的是一个微riscv处理器核(tinyriscv),用verilog语言编写,只求以最简单.最通俗易懂的方式实现riscv指令的功能,因此没有特意去对代码做任何的优化,因此你会看到里面写的代 ...
- Java自学-I/O 数据流
Java 数据流DataInputStream ,DataOutputStream DataInputStream 数据输入流 DataOutputStream 数据输出流 步骤 1 : 直接进行字符 ...
- OceanBase 架构初探
OceanBase 架构初探 原创衣舞晨风 发布于2018-11-13 08:44:14 阅读数 1417 收藏 展开 1.设计思路 OceanBase的目标是支持数百TB的数据量以及数十万TPS. ...
- 用Toad for Oracle创建数据库表空间和用户
打开Toad, 1,菜单栏Session—>new Connection….打开如下窗口: 2,进入之后,菜单DatebaseàSechema Brower...找到Table Space(表 ...
- linux 添加用户并设置主目录,shell 并赋予权限 (以 fedora 和 ubuntu 为例)
环境 centos 7.6 添加用户: [root@localhost ~]# useradd -d /home/yaoxu -m -s /bin/bash yaoxu 更改用户密码: passwd ...
- nginx 文件服务器配置,模板配置文件,有注释
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/e ...