Created
January 15, 2020 13:07
-
-
Save fengyitsai/de6c4047c66ff6dd2944cdf6242ce1f1 to your computer and use it in GitHub Desktop.
841. Keys and Rooms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
func canVisitAllRooms(_ rooms: [[Int]]) -> Bool { | |
var visited = Set<Int>() | |
var queue = [0] | |
while !queue.isEmpty { | |
var nextQueue = [Int]() | |
for room in queue where !visited.contains(room) { | |
nextQueue.append(contentsOf: rooms[room]) | |
visited.insert(room) | |
} | |
queue = nextQueue | |
} | |
return visited.count == rooms.count | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment