【LeetCode】841. Keys and Rooms 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/keys-and-rooms/description/
题目描述
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.
Note:
- 1 <= rooms.length <= 1000
- 0 <= rooms[i].length <= 1000
- The number of keys in all rooms combined is at most 3000.
题目大意
这个题目有点长,简单的翻译就是,我们有很多房间,每个房间里面有几个钥匙,每个钥匙是个数字对应着能开的房间的索引号。刚开始的时候只有第0个位置的房间是开着的,其他房间是锁着的。开了的门不会再锁上,可以允许后退。看到最后能不能把所有的房间的门都打开。
解题方法
DFS
看到这个题,我们发现最后要求出是否存在一个解。这个解是通过一段深度遍历求得。
所以很快的写出来一段dfs,dfs里把当前的门打开,并看这个房间的钥匙,找到还没去过的房间,把门打开,依次类推。
这样,我们就遍历了所有的能去到的房间,最后看一下是否所有的房间都经历过即可。
class Solution:
def canVisitAllRooms(self, rooms):
"""
:type rooms: List[List[int]]
:rtype: bool
"""
visited = [0] * len(rooms)
self.dfs(rooms, 0, visited)
return sum(visited) == len(rooms)
def dfs(self, rooms, index, visited):
visited[index] = 1
for key in rooms[index]:
if not visited[key]:
self.dfs(rooms, key, visited)
C++代码如下:
class Solution {
public:
bool canVisitAllRooms(vector<vector<int>>& rooms) {
int N = rooms.size();
vector<int> visited(N);
dfs(visited, rooms, 0);
int res = 0;
for (int v : visited) res += v;
return res == N;
}
private:
void dfs(vector<int>& visited, vector<vector<int>>& rooms, int pos) {
visited[pos] = 1;
for (int n : rooms[pos])
if (!visited[n])
dfs(visited, rooms, n);
}
};
BFS
使用BFS同样也可以,只要走不下去了,那么就停止。
class Solution {
public:
bool canVisitAllRooms(vector<vector<int>>& rooms) {
int N = rooms.size();
vector<int> visited(N);
queue<int> q;
q.push(0);
while (!q.empty()) {
int f = q.front(); q.pop();
if (visited[f]) continue;
visited[f] = 1;
for (int n : rooms[f]) {
q.push(n);
}
}
int res = 0;
for (int v : visited) res += v;
return res == N;
}
};
日期
2018 年 5 月 28 日 —— 太阳真的像日光灯~
2018 年 12 月 6 日 —— 周四啦!
【LeetCode】841. Keys and Rooms 解题报告(Python & C++)的更多相关文章
- LeetCode 841. Keys and Rooms
原题链接在这里:https://leetcode.com/problems/keys-and-rooms/ 题目: There are N rooms and you start in room 0. ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】252. Meeting Rooms 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
随机推荐
- 学习Java的第四天
一.今日收获 1.java完全手册的第一章 2. 1.6节了解了怎么样用记事本开发java程序 与用Eclipse开发 2.完成了对应例题 二.今日难题 1.一些用法容易与c++的混淆 2.语句还 ...
- 23. 关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
原文:https://blog.csdn.net/u011596455/article/details/60322568 版权声明:本文为博主原创文章,转载请附上博文链接! 在Ubuntu中,有时候运 ...
- Spark集群环境搭建——服务器环境初始化
Spark也是属于Hadoop生态圈的一部分,需要用到Hadoop框架里的HDFS存储和YARN调度,可以用Spark来替换MR做分布式计算引擎. 接下来,讲解一下spark集群环境的搭建部署. 一. ...
- 练习1--爬取btc论坛的title和相应的url
爬不到此论坛的html源码,应该涉及到反爬技术,以后再来解决,代码如下 import requests from lxml import etree import json class BtcSpid ...
- rem.js,移动多终端适配
window.onload = function(){ /*720代表设计师给的设计稿的宽度,你的设计稿是多少,就写多少;100代表换算比例,这里写100是 为了以后好算,比如,你测量的一个宽度是10 ...
- OC-基础数据类型
七 字符串与基本数据类型转换 获取字符串的每个字符/字符串和其他数据类型转换 八 NSMutableString 基本概念/常用方法 九 NSArray NSArray基本概念/创建方式/注意事项/常 ...
- Google Guava 常用集合方法
/** * Author: momo * Date: 2018/6/7 * Description: */ public class ListTest { public static void mai ...
- spring boot 配置属性值获取注解@Value和@ConfigurationProperties比较
功能比较 : @ConfigurationProperties @Value 映射赋值 批量注入配置文件中的属性 一个个指定 松散绑定(松散语法)① 支持 不支持 SpEL② 不支持 支持 ...
- Log4j2 Jndi 漏洞原理解析、复盘
" 2021-12-10一个值得所有研发纪念的日子." 一波操作猛如虎,下班到了凌晨2点25. 基础组件的重要性,在此次的Log4j2漏洞上反应的淋漓尽致,各种"核弹级漏 ...
- C#和.NET 框架
C#和.NET框架 在.NET之前 20世纪90年代,微软平台多数程序员使用VB.C或C++.他们使用的技术都有问题. 技术 问题 纯Win32 API 不是面向对象,工作量比MFC大 MFC(Mic ...