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, 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.
"""
"""
有两种方法,分别是BFS和DFS
解法一:BFS
用一个queue来存能到达的room
用一个set来存能拿到的房间钥匙
返回值为set里的钥匙数 和 房间数是否相等
"""
class Solution1:
def canVisitAllRooms(self, rooms):
queue = [0]
s = set()
s.add(0) #!!!bug 没有初始化开0门的钥匙
while queue:
keys = queue.pop()
for key in rooms[keys]:
if key not in s:
s.add(key)
queue.append(key)
return len(s) == len(rooms) """
解法二:DFS
建立一个set存钥匙,从rooms[0]开始递归
将找到的key存入set里
继续递归访问rooms[key]
"""
class Solution2:
def canVisitAllRooms(self, rooms):
s = set()
s.add(0)
def enterroom(keys):
for key in keys:
if key not in s:
s.add(key)
enterroom(rooms[key])
# else:
# pass
return
enterroom(rooms[0])
return len(s) == len(rooms)
leetcode841 Keys and Rooms的更多相关文章
- 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 ...
- [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] 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
题目: 有 N 个房间,开始时你位于 0 号房间.每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间. 在形式上,对于每个房间 i 都有一个钥匙列表 ...
- 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 ...
- LeetCode题解之Keys and Rooms
1.题目描述 2.问题分析 使用深度优先遍历 3.代码 bool canVisitAllRooms(vector<vector<int>>& rooms) { int ...
- 841. Keys and Rooms —— weekly contest 86
题目链接:https://leetcode.com/problems/keys-and-rooms/description/ 简单DFS time:9ms 1 class Solution { 2 p ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 吴裕雄--天生自然Numpy库学习笔记:NumPy 从已有的数组创建数组
import numpy as np x = [1,2,3] a = np.asarray(x) print (a) import numpy as np x = (1,2,3) a = np.asa ...
- WLC开机卡在launching....(变砖)
1.出现故障的原因:A.通过手动更换镜像导致Boot Loader Menu Run primary image (7.0.220.0) - ActiveRun backup image (7.0.2 ...
- Java学习资源 - J2EE
java Web开发基础(一)工程项目文档结构 ========rmi=========== Java RMI 框架(远程方法调用) java RMI原理详解 深究Java中的RMI底层原理 ==== ...
- 【JQuery 选择器】 案例
1.查找以id的某个字段开头的元素 setTimeout(function () { $("a[id^='menu_']").each(function () { $(this). ...
- [运维] 请求 nginx 出现 502 Bad Gateway 的解决方案!
环境: 云服务器镜像 Linux CentOS 7.6 已经安装并成功配置 SSL 的 nginx 1.16.1 成功安装并且可以正常运行的 apache-tomcat-9.0.26 遇到的问题: 在 ...
- JAVA笔记---方法
JAVA的方法 方法的基础 1. return 语句的一些高级应用 public class Method{ public static void main(Sting[] args){ System ...
- Linux中常用命令的使用(一)
这次只讲常用命令 先说命令的组成:命令一般由 (选项.命令.参数) 组成 下面就从开启一个Ubuntu开始说起 1.用户登录:在putty环境下,输完用户名在输入密码 别人想知道你用的linux系统 ...
- Web--Utils
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- hadoop启动报错处理
1. hadoop启动报错 1.1. 问题1 util.NativeCodeLoader: Unable to load native-hadoop library for your ...
- C#多态学习总结
面向对象编程三大特点 封装 继承 多态.今天我把自己学习多态的过程进行总结 多态 就是 同一个方法在不同情况下,会表选不同的效果(多个形态).在代码上表现就是 同一个父类对象 赋予不同的子类对象 就 ...