# Leetcode 841. Keys and Rooms ## DFS soluction 使用堆棧(stack)走訪無向圖 並使用 visited 陣列紀錄是否已經走訪過 如果 visited 陣列最後沒有 False 就代表房間全部走訪完畢 ```python= class Solution: def canVisitAllRooms(self, rooms: List[List[int]]) -> bool: visited = [False for i in range(len(rooms))] visited[0] = True stack = [0] while stack: room = stack.pop() keys = rooms[room] for key in keys: if not visited[key]: stack.append(key) visited[key] = True return all(visited) ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up