原题链接在这里: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. 1 <= rooms.length <= 1000
  2. 0 <= rooms[i].length <= 1000
  3. 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的更多相关文章

  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. 【LeetCode】841. Keys and Rooms 解题报告(Python & C++)

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

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

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

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

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

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

  6. LeetCode题解之Keys and Rooms

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

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

  8. [LeetCode] 4 Keys Keyboard 四键的键盘

    Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...

  9. [LeetCode] 2 Keys Keyboard 两键的键盘

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...

随机推荐

  1. Word页眉实现首页不同、奇偶页不同 、更改页眉横线、页眉文字对齐 -- 视频教程(8)

    1. 目标 目标1:实现页眉"首页不同,奇偶页不同" 目标2:更改页眉横线 目标3:页眉文字有三部分:第一部分左对齐,第二部分居中,第三部分右对齐 2. 教程 未完 ...... ...

  2. thinkphp5 模板url标签 跟javascript ajax 的 url 参数 被莫名替换

    发现一个  thinkphp5 的小bug 我用的是 thinkphp5.0.24 版本 在模板标签里 原来的大U函数  被改成url 那么问题来了   在javascript里  这样写  标签很容 ...

  3. day23——继承

    day23 初识继承 字面意思:儿子可以完全使用父亲的所有内容 专业角度:如果B类继承A类, B类就称为子类.派生类 A类就称为父类.基类.超类 面向对象三大特性:继承.封装.多态 继承:单继承.多继 ...

  4. Python之路【第三十篇】:django 模型层-多表关系

    多表操作 文件为 ---->  orm2 数据库表关系之关联字段与外键约束 一对多Book id title price publish email addr 1 php 100 人民出版社 1 ...

  5. LeetCode第154场周赛(Java)

    估计要刷很久才能突破三道题了.还是刷的太少.尽管对了前两题,但是我觉得写的不怎么样.还是将所有题目都写一下吧. 5189. "气球" 的最大数量 题目比较简单.就是找出一个字符串中 ...

  6. 深度学习-Wasserstein GAN论文理解笔记

    GAN存在问题 训练困难,G和D多次尝试没有稳定性,Loss无法知道能否优化,生成样本单一,改进方案靠暴力尝试 WGAN GAN的Loss函数选择不合适,使模型容易面临梯度消失,梯度不稳定,优化目标不 ...

  7. yii框架无限极分类的做法

    用yii框架做了一个无限极分类,主要的数组转换都是粘贴的别人的代码,但还是不要脸的写出来,方便以后自己看 用的是递归,不是path路径 控制器: protected function subtree( ...

  8. 分布式缓存重建并发冲突和zookeeper分布式锁解决方案

    如果缓存服务在本地的ehcache中都读取不到数据. 这个时候就意味着,需要重新到源头的服务中去拉去数据,拉取到数据之后,赶紧先给nginx的请求返回,同时将数据写入ehcache和redis中 分布 ...

  9. ELK学习笔记之配置logstash消费kafka多个topic并分别生成索引

    0x00 filebeat配置多个topic filebeat.prospectors: - input_type: log encoding: GB2312 # fields_under_root: ...

  10. vsftp 常见配置测试与故障排除

    匿名用户 /var/ftp        本地用户 /home/username配置vsftpd时,强烈建议·# cp /etc/vsftpd.conf /etc/vsftpd.conf1       ...