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 <= 1000
0 <= 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 ...
随机推荐
- 【C++/C】指针基本用法简介-A Summary of basic usage of Pointers.
基于谭浩强老师<C++程序设计(第三版)>做简要Summary.(2019-07-24) 一.数组与指针 1. 指针数组 一个数组,其元素均为指针类型数据,该数组称为指针数组.(type_ ...
- C++中const限定符
const基础 C++中的const,用于定义一个常量,这个常量的值不能被修改.因为const对象一旦创建就不能修改,所以const对象必须初始化.const常量特征仅仅在执行改变其本身的操作时才会发 ...
- day55——django引入、小型django(socket包装的服务器)
day55 吴超老师Django总网页:https://www.cnblogs.com/clschao/articles/10526431.html 请求(网址访问,提交数据等等) request 响 ...
- Python之路【第十五篇】开发FTP多线程程序
要求: 1.用户加密认证 2.允许同时多用户登录 3.每个用户有自己的家目录,且只能访问自己的家目录 4.对用户进行磁盘配额,每个用户的可用空间不同 5.允许用户在ftp server上随意切换目录 ...
- Golang 读写文件
读文件 func ReadFile_v1(filename string) { var ( err error content []byte ) fileObj,err := os.Open(file ...
- Golang 常用的第三方包.
Goland 下面这个license server 可用 http://idea.youbbs.org (2018-01-10 04:26:09) http://45.77.127.87:81(201 ...
- java基础 String
标准格式:数据类型[] 数组名称 = new 数据类型[] {元素1,元素2,...};省略格式:数据类型[] 数组名称 = {元素1,元素2,...}; Scanner类实现的功能,可以实现键盘输入 ...
- docker部署Asp.Net Core、Nginx、MySQL
2019/10/24,docker19.03.4, .netcore 3.0,CentOS7.6 摘要:asp.net core 3.0 网站项目容器化部署,使用docker-compose编排Ngi ...
- iOS - FlexBox 布局之 YogaKit
由于刚开始的项目主要用的H5.javaScript技术为主原生开发为辅的手段开发的项目,UI主要是还是H5,如今翻原生.为了方便同时维护两端.才找到这个很不错的库. FlexBox?听起来像是一门H5 ...
- VUE过滤器 基础回顾5
过滤器是一种在模板中处理数据的便捷方式,特别适合对字符串和数组进行简易显示 <div id="app"> <p>商品1花费{{oneCost | froma ...