Skip to content

Commit c227864

Browse files
committed
feat: add solutions to lc problem: No.0841
No.0841.Keys and Rooms
1 parent fd093f8 commit c227864

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

solution/0800-0899/0841.Keys and Rooms/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,62 @@ func canVisitAllRooms(rooms [][]int) bool {
149149
}
150150
```
151151

152+
### **TypeScript**
153+
154+
```ts
155+
function canVisitAllRooms(rooms: number[][]): boolean {
156+
const n = rooms.length;
157+
const isOpen = new Array(n).fill(false);
158+
const dfs = (i: number) => {
159+
if (isOpen[i]) {
160+
return;
161+
}
162+
isOpen[i] = true;
163+
rooms[i].forEach(k => dfs(k));
164+
};
165+
dfs(0);
166+
return isOpen.every(v => v);
167+
}
168+
```
169+
170+
```ts
171+
function canVisitAllRooms(rooms: number[][]): boolean {
172+
const n = rooms.length;
173+
const isOpen = new Array(n).fill(false);
174+
const keys = [0];
175+
while (keys.length !== 0) {
176+
const i = keys.pop();
177+
if (isOpen[i]) {
178+
continue;
179+
}
180+
isOpen[i] = true;
181+
keys.push(...rooms[i]);
182+
}
183+
return isOpen.every(v => v);
184+
}
185+
```
186+
187+
### **Rust**
188+
189+
```rust
190+
impl Solution {
191+
pub fn can_visit_all_rooms(rooms: Vec<Vec<i32>>) -> bool {
192+
let n = rooms.len();
193+
let mut is_open = vec![false; n];
194+
let mut keys = vec![0];
195+
while !keys.is_empty() {
196+
let i = keys.pop().unwrap();
197+
if is_open[i] {
198+
continue;
199+
}
200+
is_open[i] = true;
201+
rooms[i].iter().for_each(|&key| keys.push(key as usize));
202+
}
203+
is_open.iter().all(|&v| v)
204+
}
205+
}
206+
```
207+
152208
### **...**
153209

154210
```

solution/0800-0899/0841.Keys and Rooms/README_EN.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,62 @@ func canVisitAllRooms(rooms [][]int) bool {
136136
}
137137
```
138138

139+
### **TypeScript**
140+
141+
```ts
142+
function canVisitAllRooms(rooms: number[][]): boolean {
143+
const n = rooms.length;
144+
const isOpen = new Array(n).fill(false);
145+
const dfs = (i: number) => {
146+
if (isOpen[i]) {
147+
return;
148+
}
149+
isOpen[i] = true;
150+
rooms[i].forEach(k => dfs(k));
151+
};
152+
dfs(0);
153+
return isOpen.every(v => v);
154+
}
155+
```
156+
157+
```ts
158+
function canVisitAllRooms(rooms: number[][]): boolean {
159+
const n = rooms.length;
160+
const isOpen = new Array(n).fill(false);
161+
const keys = [0];
162+
while (keys.length !== 0) {
163+
const i = keys.pop();
164+
if (isOpen[i]) {
165+
continue;
166+
}
167+
isOpen[i] = true;
168+
keys.push(...rooms[i]);
169+
}
170+
return isOpen.every(v => v);
171+
}
172+
```
173+
174+
### **Rust**
175+
176+
```rust
177+
impl Solution {
178+
pub fn can_visit_all_rooms(rooms: Vec<Vec<i32>>) -> bool {
179+
let n = rooms.len();
180+
let mut is_open = vec![false; n];
181+
let mut keys = vec![0];
182+
while !keys.is_empty() {
183+
let i = keys.pop().unwrap();
184+
if is_open[i] {
185+
continue;
186+
}
187+
is_open[i] = true;
188+
rooms[i].iter().for_each(|&key| keys.push(key as usize));
189+
}
190+
is_open.iter().all(|&v| v)
191+
}
192+
}
193+
```
194+
139195
### **...**
140196

141197
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn can_visit_all_rooms(rooms: Vec<Vec<i32>>) -> bool {
3+
let n = rooms.len();
4+
let mut is_open = vec![false; n];
5+
let mut keys = vec![0];
6+
while !keys.is_empty() {
7+
let i = keys.pop().unwrap();
8+
if is_open[i] {
9+
continue;
10+
}
11+
is_open[i] = true;
12+
rooms[i].iter().for_each(|&key| keys.push(key as usize));
13+
}
14+
is_open.iter().all(|&v| v)
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function canVisitAllRooms(rooms: number[][]): boolean {
2+
const n = rooms.length;
3+
const isOpen = new Array(n).fill(false);
4+
const keys = [0];
5+
while (keys.length !== 0) {
6+
const i = keys.pop();
7+
if (isOpen[i]) {
8+
continue;
9+
}
10+
isOpen[i] = true;
11+
keys.push(...rooms[i]);
12+
}
13+
return isOpen.every(v => v);
14+
}

0 commit comments

Comments
 (0)