Skip to content

Commit c782037

Browse files
committed
一刷841
1 parent 8fefb81 commit c782037

File tree

5 files changed

+102
-41
lines changed

5 files changed

+102
-41
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5912,14 +5912,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
59125912
//|{doc_base_url}/0840-magic-squares-in-grid.adoc[题解]
59135913
//|Easy
59145914
//|
5915-
//
5916-
//|{counter:codes}
5917-
//|{leetcode_base_url}/keys-and-rooms/[841. Keys and Rooms^]
5918-
//|{source_base_url}/_0841_KeysAndRooms.java[Java]
5919-
//|{doc_base_url}/0841-keys-and-rooms.adoc[题解]
5920-
//|Medium
5921-
//|
5922-
//
5915+
5916+
|{counter:codes}
5917+
|{leetcode_base_url}/keys-and-rooms/[841. Keys and Rooms^]
5918+
|{source_base_url}/_0841_KeysAndRooms.java[Java]
5919+
|{doc_base_url}/0841-keys-and-rooms.adoc[题解]
5920+
|Medium
5921+
|
5922+
59235923
//|{counter:codes}
59245924
//|{leetcode_base_url}/split-array-into-fibonacci-sequence/[842. Split Array into Fibonacci Sequence^]
59255925
//|{source_base_url}/_0842_SplitArrayIntoFibonacciSequence.java[Java]

docs/0841-keys-and-rooms.adoc

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,78 @@
11
[#0841-keys-and-rooms]
2-
= 841. Keys and Rooms
2+
= 841. 钥匙和房间
33

4-
{leetcode}/problems/keys-and-rooms/[LeetCode - Keys and Rooms^]
4+
https://leetcode.cn/problems/keys-and-rooms/[LeetCode - 841. 钥匙和房间 ^]
55

6-
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.
6+
`n` 个房间,房间按从 `0``n - 1` 编号。最初,除 `0` 号房间外的其余所有房间都被锁住。你的目标是进入所有的房间。然而,你不能在没有获得钥匙的时候进入锁住的房间。
77

8-
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`.
8+
当你进入一个房间,你可能会在里面找到一套 *不同的钥匙*,每把钥匙上都有对应的房间号,即表示钥匙可以打开的房间。你可以拿上所有钥匙去解锁其他房间。
99

10-
Initially, all the rooms start locked (except for room `0`).
10+
给你一个数组 `rooms` 其中 `rooms[i]` 是你进入 `i` 号房间可以获得的钥匙集合。如果能进入 *所有* 房间返回 `true`,否则返回 `false`
1111

12-
You can walk back and forth between rooms freely.
1312

14-
Return `true` if and only if you can enter every room.
13+
*示例 1:*
1514

15+
....
16+
输入:rooms = [[1],[2],[3],[]]
17+
输出:true
18+
解释:
19+
我们从 0 号房间开始,拿到钥匙 1。
20+
之后我们去 1 号房间,拿到钥匙 2。
21+
然后我们去 2 号房间,拿到钥匙 3。
22+
最后我们去了 3 号房间。
23+
由于我们能够进入每个房间,我们返回 true。
24+
....
1625

26+
*示例 2:*
1727

28+
....
29+
输入:rooms = [[1,3],[3,0,1],[2],[0]]
30+
输出:false
31+
解释:我们不能进入 2 号房间。
32+
....
1833

19-
*Example 1:*
2034

21-
[subs="verbatim,quotes,macros"]
22-
----
23-
*Input:* [[1],[2],[3],[]]
24-
*Output:* true
25-
*Explanation: *
26-
We start in room 0, and pick up key 1.
27-
We then go to room 1, and pick up key 2.
28-
We then go to room 2, and pick up key 3.
29-
We then go to room 3. Since we were able to go to every room, we return true.
30-
----
31-
32-
*Example 2:*
33-
34-
[subs="verbatim,quotes,macros"]
35-
----
36-
*Input:* [[1,3],[3,0,1],[2],[0]]
37-
*Output:* false
38-
*Explanation:* We can't enter the room with number 2.
39-
----
35+
*提示:*
4036

41-
*Note:*
37+
* `n == rooms.length`
38+
* `+2 <= n <= 1000+`
39+
* `+0 <= rooms[i].length <= 1000+`
40+
* `+1 <= sum(rooms[i].length) <= 3000+`
41+
* `+0 <= rooms[i][j] < n+`
42+
* 所有 `rooms[i]` 的值 *互不相同*
4243
4344
44-
. `1 <= rooms.length <= 1000`
45-
. `0 <= rooms[i].length <= 1000`
46-
. The number of keys in all rooms combined is at most `3000`.
47-
45+
== 思路分析
4846

47+
根据钥匙,把房间走一遍即可。可以使用备忘录来防止重复走同一个房间来加快速度。
4948

49+
既可以深度优先遍历,也可以广度优先遍历。如果使用深度优先遍历,需要使用递归。
5050

5151
[[src-0841]]
52+
[tabs]
53+
====
54+
一刷::
55+
+
56+
--
5257
[{java_src_attr}]
5358
----
5459
include::{sourcedir}/_0841_KeysAndRooms.java[tag=answer]
5560
----
61+
--
62+
63+
// 二刷::
64+
// +
65+
// --
66+
// [{java_src_attr}]
67+
// ----
68+
// include::{sourcedir}/_0841_KeysAndRooms_2.java[tag=answer]
69+
// ----
70+
// --
71+
====
72+
73+
74+
== 参考资料
75+
76+
. https://leetcode.cn/problems/keys-and-rooms/solutions/393524/yao-chi-he-fang-jian-by-leetcode-solution/[841. 钥匙和房间 - 官方题解^]
77+
. https://leetcode.cn/problems/keys-and-rooms/solutions/18826/7xing-dfs-8xing-bfs-liang-chong-fang-fa-san-chong-/[841. 钥匙和房间 - 7行DFS 8行BFS 两种方法 三种实现 超详细^]
5678

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,7 @@ include::0796-rotate-string.adoc[leveloffset=+1]
17651765

17661766
// include::0840-magic-squares-in-grid.adoc[leveloffset=+1]
17671767

1768-
// include::0841-keys-and-rooms.adoc[leveloffset=+1]
1768+
include::0841-keys-and-rooms.adoc[leveloffset=+1]
17691769

17701770
// include::0842-split-array-into-fibonacci-sequence.adoc[leveloffset=+1]
17711771

logbook/202503.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,12 @@ endif::[]
803803
|{doc_base_url}/1006-clumsy-factorial.adoc[题解]
804804
|✅ 栈。代码写出来了,但是好烂!
805805

806+
|{counter:codes2503}
807+
|{leetcode_base_url}/keys-and-rooms/[841. 钥匙和房间^]
808+
|{doc_base_url}/0841-keys-and-rooms.adoc[题解]
809+
|✅ 深度优先遍历或广度优先遍历。
810+
811+
806812
|===
807813

808814
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.*;
4+
5+
public class _0841_KeysAndRooms {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-05-24 18:48:35
10+
*/
11+
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
12+
Queue<Integer> queue = new LinkedList<>();
13+
queue.addAll(rooms.get(0));
14+
rooms.set(0, null);
15+
while (!queue.isEmpty()) {
16+
Integer key = queue.poll();
17+
List<Integer> keys = rooms.get(key);
18+
if (keys != null) {
19+
queue.addAll(keys);
20+
rooms.set(key, null);
21+
}
22+
}
23+
return rooms.stream().allMatch(Objects::isNull);
24+
}
25+
// end::answer[]
26+
27+
public static void main(String[] args) {
28+
;
29+
new _0841_KeysAndRooms().canVisitAllRooms(
30+
new ArrayList<List<Integer>>(List.of(List.of(1, 3), List.of(3, 0, 1), List.of(2), List.of(0)))
31+
);
32+
}
33+
}

0 commit comments

Comments
 (0)