【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 ...
随机推荐
- oracle 将电话号码中间4位数以星号*代替
select replace('17665312355',substr('17665312355',4,4),'****') as phone, #类似E ...
- 打造基于 PostgreSQL/openGauss 的分布式数据库解决方案
在 MySQL ShardingSphere-Proxy 逐渐成熟并被广泛采用的同时,ShardingSphere 团队也在 PostgreSQL ShardingSphere-Proxy 上持续发力 ...
- 巩固javaweb第十二天
巩固内容: HTML 图像- 图像标签( <img>)和源属性(Src) 在 HTML 中,图像由<img> 标签定义. <img> 是空标签,意思是说,它只包含属 ...
- FileReader (三) - 网页拖拽并预显示图片简单实现
以下是一个很贱很简单的一个 在网页上图拽图片并预显示的demo. 我是从https://developer.mozilla.org/en-US/docs/Web/API/FileReader#Stat ...
- 【leetcode】378. Kth Smallest Element in a Sorted Matrix(TOP k 问题)
Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kt ...
- 颜色RGB值对照表
转载自 http://www.91dota.com/?p=49# 常用颜色的RGB值及中英文名称 颜 色 RGB值 英文名 中文名 #FFB6C1 LightPink 浅粉红 #F ...
- Can we call an undeclared function in C++?
Calling an undeclared function is poor style in C (See this) and illegal in C++. So is passing argum ...
- Mysql配置文件 innodb引擎
目录 innodb参数 innodb_buffer_pool_size innodb_read_io_threads|innodb_write_io_threads innodb_open_files ...
- Docker从入门到精通(四)——常用命令
话不多说,本篇文章给大家介绍 docker 的常用命令,基本上会覆盖我们日常使用的命令. 1.万能帮助命令 docker 命令 --help 假设你想用某个命令,但是又不知道该命令的一些参数怎么用,这 ...
- 简单的理解 Object.defineProperty()
Object.defineProperty()的作用就是直接在一个对象上定义一个新属性,或者修改一个已经存在的属性. Object.defineProperty(obj,prop,descriptor ...