Skip to content

Commit ffe195e

Browse files
committed
Time: 2 ms (44.3%), Space: 56 MB (78.07%) - LeetHub
source:bfc9bc2a15b1fa994c80fb0406b8cfbfb4167903
1 parent 3b6332d commit ffe195e

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[][]} rooms
3+
* @return {boolean}
4+
*/
5+
var canVisitAllRooms = function(rooms) {
6+
const visited = new Array(rooms.length).fill(false);
7+
visited[0] = true;
8+
const queue = [0];
9+
10+
let cnt = 0;
11+
12+
while(queue.length >0){
13+
const current = queue.pop();
14+
cnt++;
15+
const keys = rooms[current];
16+
17+
for(const key of keys){
18+
if(visited[key]) continue;
19+
queue.push(key);
20+
visited[key]=true;
21+
}
22+
}
23+
24+
if(cnt===rooms.length) return true;
25+
return false;
26+
27+
28+
29+
30+
31+
32+
33+
};

0 commit comments

Comments
 (0)