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 ...
随机推荐
- Jmeter之用于json格式的响应断言
当响应结果是json格式时,用JSON Assertion更方便判断. 1 在请求上右键添加json断言 2 编辑json Assertion 判断方式: 如果响应结果不是json格式的,fail ...
- 学习 正则表达式 js java c# python 通用
正则表达式 js java c# python 学习网站 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Globa ...
- SonarLint各种提示的意思
1.Refactor this method to reduce its Cognitive Complexity from 29 to the 15 allowed. 2.Method has 15 ...
- C#数组1
using System; namespace ConsoleApp3 { class Program { static void Main(string[] args) { , , , , , }; ...
- Flask 教程 第一章:Hello, World!
本文翻译自The Flask Mega-Tutorial Part I: Hello, World! 一趟愉快的学习之旅即将开始,跟随它你将学会用Python和Flask来创建Web应用.上面的视频包 ...
- Java生鲜电商平台-深入订单拆单架构与实战
Java生鲜电商平台-深入订单拆单架构与实战 Java生鲜电商中在做拆单的需求,细思极恐,思考越深入,就会发现里面涉及的东西越来越多,要想做好订单拆单的功能,还是相当有难度, 因此总结了一下拆单功能细 ...
- fwrite(): send of 8192 bytes failed with errno=104 Connection reset by peer
问题:fwrite(): send of 8192 bytes failed with errno=104 Connection reset by peer 问题描述 通过mysql + sphinx ...
- SSM框架之Mybatis(2)CRUD操作
Mybatis(2)CRUD 1.基于代理Dao实现CRUD操作 使用要求: 1.持久层接口(src\main\java\dao\IUserDao.java)和持久层接口的映射配置(src\main\ ...
- 备战双十一,腾讯WeTest有高招——小程序质量优化必读
WeTest 导读 2018年双十一战场小程序购物通道表现不俗,已逐渐成为各大品牌方角逐的新战场.数据显示,截止目前95%的电商平台都已经上线了小程序.除了电商企业外,许多传统线下商家也开始重视小程序 ...
- SpringMVC入门 -- 参数绑定
一.REST与RESTful 1.简介 (1)REST(Representational State Transfer):表现层状态转移,一种软件架构风格,不是标准.REST描述的是在网络中clien ...