"""
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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. LeetCode 841:钥匙和房间 Keys and Rooms

    题目: ​ 有 N 个房间,开始时你位于 0 号房间.每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间. ​ 在形式上,对于每个房间 i 都有一个钥匙列表 ...

  5. LeetCode 841. Keys and Rooms

    原题链接在这里:https://leetcode.com/problems/keys-and-rooms/ 题目: There are N rooms and you start in room 0. ...

  6. 【LeetCode】841. Keys and Rooms 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  7. LeetCode题解之Keys and Rooms

    1.题目描述 2.问题分析 使用深度优先遍历 3.代码 bool canVisitAllRooms(vector<vector<int>>& rooms) { int ...

  8. 841. Keys and Rooms —— weekly contest 86

    题目链接:https://leetcode.com/problems/keys-and-rooms/description/ 简单DFS time:9ms 1 class Solution { 2 p ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. PostgreSQL数据库-分页sql--offset

    select * from users order by score desc limit 3;--取成绩的前3名=====select * from users order by score des ...

  2. 运营商如何关闭2G、3G网络?这事儿得从小灵通说起

    5G时代即将全面开启,主流声音是对未来的无限畅想--5G将带来翻天覆地的变化.不过凡事都有利弊两面性,5G作为新生事物固然大有可为,但不可避免地会对旧事物造成巨大冲击.除了会影响很多跟不上潮流发展的行 ...

  3. 微信小程序 列表倒计时

    最近要实现一个列表倒计时的功能,写了个demo 展示图 <view class="center colu"> <view class="time&quo ...

  4. VSCode 出现错误 System.IO.IOException: The configured user limit (128) on the number of inotify instances has been reached.

    方案一: sudo vim /etc/sysctl.conf 增加下面内容(环境变量) fs.inotify.max_user_watches = 1638400 fs.inotify.max_use ...

  5. PAT T1019 Separate the Animals

    暴力搜索加剪枝,二进制保存状态,set去重~ #include<bits/stdc++.h> using namespace std; ; string s[maxn]; struct n ...

  6. 登陆页面Sql注入(绕过)

    如图,看到这道题的时候发觉之前做过一个类似的手工注入: 不过这次手注会失败,后台过滤了sql语句里的一些东西,但我们并不知道过滤了什么 到这里我就基本上没辙了,不过查询了资料以后发现sqlmap可以对 ...

  7. SOAP1.1 VS SOAP1.2

    SOAP提升: 目前WebService的协议主要有SOAP1.1和1.2.两者的命名空间不同. 见下页对比 SOAP1.1版本与SOAP1.2版本在头信息上存在差异.SOAP1.1存在SOAPAct ...

  8. Mybatis plus 插入数据时将自动递增的主键手动进行赋值设置

    1.首先设置好实体类:将类型设置为 @TableId(type = IdType.INPUT) 2.在插入数据前将id赋值给实体类对象即可

  9. 并发编程之Event事件

    Event事件 用来同步线程之间的状态. 举个例子: ​ 你把一个任务丢到了子线程中,这个任务将异步执行.如何获取到这个任务的执行状态 解决方法: 如果是拿到执行结果 我们可以采用异步回调, 在这里我 ...

  10. Codeforces 599D:Spongebob and Squares

    D. Spongebob and Squares time limit per test 2 seconds memory limit per test 256 megabytes input sta ...