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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 安装插件报错error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++
起因:学到多线程greenlet模块时,greenlet模块死活装不上,以为pycharm坏掉了,浪费了一下午. #pip3 install greenlet from greenlet import ...
- [idea] 解决 idea 复制进项目的文件运行时无法找到的问题
解决方法一: Rebuild后,重启项目 解决方法二:
- Swift3.0-基础知识
本文对Swift做一个从OC的角度的基础知识简单概要. Swift OC 说明 let.var const 在OC中不用const声明的常量,都认为是变量 Float.Double CGFloat ...
- 【转】Docker学习_本地/容器文件互传(5)
将容器内文件拷贝到宿主机 docker cp <containerId>:/导出文件的位置/xxx.sql /宿主机的位置 示例:docker cp bf4c4fff338c:/root/ ...
- 远程登陆ubantu服务器 .bashrc文件每次打开终端都需要source的问题
通过创建的用户登录ubantu服务器时,.bashrc文件每次都要重新配置,要不然里面的配置如命令的简写如 ll 等就无法识别,本方法用于实现登录时自动执行.bashrc文件. 1.ubantu启动时 ...
- CDQ分治-陌上花开(附典型错误及原因)
CDQ分治-陌上花开 题目大意 对于给遗传给定的序列: \[ (x,y,z)_1, (x,y,z)_2, (x,y,z)_3, \cdots, (x,y,z)_n \] 求: \[ \sum_{x_i ...
- 「SPOJ10707」Count on a tree II
「SPOJ10707」Count on a tree II 传送门 树上莫队板子题. 锻炼基础,没什么好说的. 参考代码: #include <algorithm> #include &l ...
- Django 学习 之 视图层(views)
一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. . . 是任何 ...
- Controller生命周期
1. 实例化 alloc/init, initWithNibName 2.awakeFromNib 从nib创建Controller对象 3.get/set outlets 4. viewDidLoa ...
- express框架开发接口部署线上环境PM2
1.PM2介绍 PM2是一个线上环境下,用于启动nodejs进程守护的工具,用来保证服务的稳定及分摊服务器进程和压力. 2.下载安装 npm install pm2 -g => pm2 --v ...