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 <= 1000
0 <= rooms[i].length <= 1000
- 所有房间中的钥匙数量总计不超过
3000
。
Note:
1 <= rooms.length <= 1000
0 <= 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 ...
随机推荐
- 感受一把面试官通过一道题目引出的关于 TCP 的 5 个连环炮!
面试现场:从 URL 在浏览器被被输入到页面展现的过程中发生了什么? 相信大多数准备过的同学都能回答出来,但是如果继续问:收到的 HTML 如果包含几十个图片标签,这些图片是以什么方式什么顺序下载?建 ...
- Java菜题
编程语言:Java 2019年全国高校计算机能力挑战赛分设大数据算法赛(所谓的内部试题) 一.选择题(共15题,每题3分,共45分) 1. 在Java中下列说法正确的是( ) A.一个子类可以有多 ...
- SqlServer,Oracle,db2,MySql查询表索引
SqlServer1. 查询表索引 SELECT 索引名称=a.name ,表名=c.name ,索引字段名=d.name ,索引字段位置=d.colid ,c.status FROM sysinde ...
- MySQL入门——在Linux下安装和卸载MySQL
MySQL入门——在Linux下安装和卸载MySQL 摘要:本文主要学习了如何在Linux系统中安装和卸载MySQL数据库. 查看有没有安装过MySQL 使用命令查看有没有安装过: [root@loc ...
- git 多账户链接不同gitlab仓库
1.若之前对 git 设置过全局的 user.name 和 user.email.类似(用git config --global --list 进行查看你是否设置) 一定要清除之前设置的用户和邮箱 $ ...
- SSM框架之Mybatis(1)入门
Mybatis(1)入门 1.mybatis的概述 mybatis是一个持久层框架,用java编写的. 它封装了jdbc操作的很多细节,使开发者只需要关注sql语句本身,而无需关注注册驱动,创建连接等 ...
- Redis Python(一)
Infi-chu: http://www.cnblogs.com/Infi-chu/ NoSQL(NoSQL=Not Only SQL),中文意思是非关系型数据库. 随着互联网Web2.0网站的兴起, ...
- 安卓开发笔记(三十二):banner轮播图的实现
一.activity.xml 我这里主要爬取的爱奇艺首页的图片进行轮播,应用了两个github上的开源库,一个banner的库,一个加载网络图片的库,用开源库能够极大地节省我们编写代码的时间. < ...
- STM32 IAP 升级功能
IAP In Application Programming 可通过USB,CAN,UART,I2C,SPI等接口实现 IAP流程 Bootloader程序:接收升级程序,更新到flash指定地址:跳 ...
- 渗透测试学习 十三、 SQLmap使用详解
SQLmap介绍 sqlmap是一个由python语言编写的开源的渗透测试工具,它主要是检测SQL注入漏洞,是一款功能强大的SQL漏洞检测利用工具. 他可以检测的数据库有:access.msSQL.M ...