LeetCode 841. Keys and Rooms
原题链接在这里:https://leetcode.com/problems/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.
Note:
1 <= rooms.length <= 10000 <= rooms[i].length <= 1000- The number of keys in all rooms combined is at most
3000.
题解:
Could traverse rooms by BFS.
Get key lists, if not visited before, add it into queue.
Check visited rooms count == N.
Time Complexity: O(V+E). V = total number of rooms. E = total number of keys.
Space: O(V).
AC Java:
class Solution {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
if(rooms == null || rooms.size() == 0){
return true;
}
int n = rooms.size();
boolean [] visited = new boolean[n];
LinkedList<Integer> que = new LinkedList<Integer>();
visited[0] = true;
que.add(0);
int res = 0;
while(!que.isEmpty()){
int cur = que.poll();
res++;
for(int key : rooms.get(cur)){
if(!visited[key]){
visited[key] = true;
que.add(key);
}
}
}
return res == n;
}
}
Could iterate rooms by DFS too.
Time Complexity: O(V+E).
Space: O(V). stack space.
AC Java:
class Solution {
int res = 0;
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
if(rooms == null || rooms.size() == 0){
return true;
}
int n = rooms.size();
boolean [] visited = new boolean[n];
dfs(0, rooms, visited);
return res == n;
}
private void dfs(int cur, List<List<Integer>> rooms, boolean [] visited){
if(visited[cur]){
return;
}
visited[cur] = true;
res++;
for(int key : rooms.get(cur)){
dfs(key, rooms, visited);
}
}
}
LeetCode 841. 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 ...
- 【LeetCode】841. Keys and Rooms 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 841. Keys and Rooms —— weekly contest 86
题目链接:https://leetcode.com/problems/keys-and-rooms/description/ 简单DFS time:9ms 1 class Solution { 2 p ...
- LeetCode 841:钥匙和房间 Keys and Rooms
题目: 有 N 个房间,开始时你位于 0 号房间.每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间. 在形式上,对于每个房间 i 都有一个钥匙列表 ...
- [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题解之Keys and Rooms
1.题目描述 2.问题分析 使用深度优先遍历 3.代码 bool canVisitAllRooms(vector<vector<int>>& rooms) { int ...
- [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] 4 Keys Keyboard 四键的键盘
Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...
- [LeetCode] 2 Keys Keyboard 两键的键盘
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
随机推荐
- DRF框架(四)——单整体改(put)、单局部改(patch)、群局部改(patch)
单整体改 单指的是单独一条数据,整体指这条数据的所有字段都必须传值修改 基于上篇文章的代码修改,序列化层不用变,只修改views.py 1) 单整体改,说明前台要提供修改的数据,那么数据就需要校验 ...
- php的json_encode第二个参数学习及应用
php5.4以上: json_encode($data, JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); p ...
- 2019vivo秋招提前批笔试题第3题
笔试的时候没做出来,就顺手截图了. 虽然知道要用动态规划做,但我一直就不太懂动态规划.笔试完又花了2小时把它做出来了.也不知道性能怎么样,但还好做出来了. def solution(n, toltal ...
- loj#10012\poj2018 Best Cow Fences(二分)
题目 #10012 「一本通 1.2 例 2」Best Cow Fences 解析 有序列\(\{a_i\}\),设\([l,r]\)上的平均值为\(\bar{x}\),有\(\sum_{i=l}^r ...
- 使pre的内容自动换行(转)小知识
<pre> 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见应用就是用来表示计算机的源代码 ...
- python基础之猜数字游戏
#猜数字游戏 import random #impor语句导入random模块 guessor=0; print("#"*30) #输出30个”#“(”############## ...
- JS中的七大数据类型
js中有7种数据类型,包括五种基本数据类型(Number,String,Boolean,Undefined,Null),和一种复杂数据类型(Object)以及es6语法新增的Symbol数据类型 es ...
- Windows VNC远程连接用法
VNC (Virtual Network Console)是虚拟网络控制台 被控端 被控端需要打开服务,等待主控端连接 服务端已经启动成功,右下角有小图标 主控端 打开主控端,连接被控端 输入被控端i ...
- jhipster技术栈研究
背景: 公司新的微服务项目都用jhipster脚手架来开发,这篇博客是jhipster里面涉及到技术的汇总目录 一.官方文档中涉及到的技术栈 前端技术栈 Angular / React / Vue R ...
- Python爬虫系列:五、正则表达式
1.了解正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. 正则表达式 ...