在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ There are Formally, each room Initially, all the rooms start locked (except for room You can walk back and forth between rooms freely. Return Example 1: Input: [[1],[2],[3],[]] Output: true Explanation: We start in room 0, and pick up key 1. We then go to room 1, and pick up key 2. We then go to room 2, and pick up key 3. We then go to room 3. Since we were able to go to every room, we return true. Example 2: Input: [[1,3],[3,0,1],[2],[0]] Output: false Explanation: We can't enter the room with number 2. Note:
有 在形式上,对于每个房间 最初,除 你可以自由地在房间之间来回走动。 如果能进入每个房间返回 示例 1: 输入: [[1],[2],[3],[]] 输出: true 解释: 我们从 0 号房间开始,拿到钥匙 1。 之后我们去 1 号房间,拿到钥匙 2。 然后我们去 2 号房间,拿到钥匙 3。 最后我们去了 3 号房间。 由于我们能够进入每个房间,我们返回 true。 示例 2: 输入:[[1,3],[3,0,1],[2],[0]] 输出:false 解释:我们不能进入 2 号房间。 提示:
40ms 1 class Solution { 2 func canVisitAllRooms(_ rooms: [[Int]]) -> Bool { 3 var keys: [Int] = Array(repeating: -1, count: rooms.count) 4 var result:[Int] = [0] 5 keys[0] = 0 6 var i: Int = 0 7 while i < result.count { 8 for each in rooms[result[i]] { 9 if keys[each] == -1 { 10 result.append(each) 11 keys[each] = 0 12 } 13 } 14 if result.count == rooms.count { 15 return true 16 } 17 i += 1 18 } 19 return false 20 } 21 } Runtime: 44 ms
Memory Usage: 19.4 MB
1 class Solution { 2 func canVisitAllRooms(_ rooms: [[Int]]) -> Bool { 3 var dfs:[Int] = [Int]() 4 dfs.append(0) 5 var seen:Set<Int> = Set<Int>() 6 seen.insert(0) 7 while(!dfs.isEmpty) 8 { 9 var i:Int = dfs.removeLast() 10 for j in rooms[i] 11 { 12 if !seen.contains(j) 13 { 14 dfs.append(j) 15 seen.insert(j) 16 if rooms.count == seen.count {return true} 17 } 18 } 19 } 20 return rooms.count == seen.count 21 } 22 } 44ms 1 class Solution { 2 func canVisitAllRooms(_ rooms: [[Int]]) -> Bool { 3 let count = rooms.count 4 var seen = [Bool](repeating: false, count: count) 5 seen[0] = true 6 7 var keysInRoom = [Int]() 8 keysInRoom.append(0) 9 10 while keysInRoom.count != 0 { 11 let room = keysInRoom.last! 12 keysInRoom.removeLast() 13 14 let keys = rooms[room] 15 for key in keys { 16 if !seen[key] { 17 seen[key] = true 18 keysInRoom.append(key) 19 } 20 } 21 } 22 for s in seen { 23 if !s { 24 return false 25 } 26 } 27 return true 28 } 29 } 48ms 1 class Solution { 2 func canVisitAllRooms(_ rooms: [[Int]]) -> Bool { 3 4 var numberOfRooms = rooms.count 5 var visited: [Int: Int] = [:] 6 var stack: [Int] = [] 7 stack.append(0) 8 visited[0] = 1 9 10 while(!stack.isEmpty) { 11 12 var room = stack.removeLast() 13 var keys = rooms[room] 14 15 for key in keys { 16 if(visited[key] == nil) { 17 visited[key] = 1 18 stack.append(key) 19 } 20 } 21 } 22 return numberOfRooms == visited.keys.count 23 } 24 } 52ms 1 class Solution { 2 func canVisitAllRooms(_ rooms: [[Int]]) -> Bool { 3 var key = Set(rooms[0]) 4 var open = [0:0] 5 var nextOpen : Int? = 0 6 while nextOpen != nil { 7 nextOpen = nil 8 for data in key { 9 if open[data] == nil { 10 nextOpen = data 11 open[ data] = data 12 for k in rooms[data]{ 13 key.insert(k) 14 } 15 key.remove(data) 16 } 17 } 18 } 19 return open.count == rooms.count 20 } 21 } 64ms 1 class Solution { 2 func canVisitAllRooms(_ rooms: [[Int]]) -> Bool { 3 var unvisited : Set<Int> = [] 4 var canVisit : [Int] = [] 5 6 for i in 0 ..< rooms.count { 7 unvisited.insert(i) 8 } 9 10 canVisit = rooms[0] 11 unvisited.remove(0) 12 13 while !canVisit.isEmpty { 14 let room = canVisit.removeFirst() 15 if unvisited.contains(room) { 16 canVisit.append(contentsOf: rooms[room]) 17 unvisited.remove(room) 18 } 19 } 20 21 if unvisited.isEmpty { 22 return true 23 } 24 25 return false 26 } 27 } 76ms 1 class Solution { 2 func canVisitAllRooms(_ rooms: [[Int]]) -> Bool { 3 if (rooms.count <= 1) { return true } 4 5 var visitedRooms: [Bool] = Array.init(repeating: false, count: rooms.count) 6 visitedRooms[0] = true 7 var queue: [Int] = [] 8 addKeysToQueue(&queue, 0, &visitedRooms, rooms) 9 10 while (!queue.isEmpty) { 11 addKeysToQueue(&queue, queue.removeLast(), &visitedRooms, rooms) 12 } 13 14 for i in 0..<visitedRooms.count { 15 if (!visitedRooms[i]) { 16 return false 17 } 18 } 19 return true 20 } 21 22 func addKeysToQueue(_ queue: inout [Int], _ key: Int, _ visitedRooms: inout [Bool], _ rooms: [[Int]]) { 23 for i in 0..<rooms[key].count { 24 var nextRoom = rooms[key][i] 25 if (!visitedRooms[nextRoom]) { 26 queue.append(nextRoom) 27 visitedRooms[nextRoom] = true 28 } 29 } 30 } 31 }
|
请发表评论