Created
May 30, 2018 13:23
-
-
Save s4553711/05b85d622603cf915bd2e688979f30a2 to your computer and use it in GitHub Desktop.
This file contains 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 { | |
public: | |
bool canVisitAllRooms(vector<vector<int>>& rooms) { | |
int numrooms = rooms.size(); | |
vector<bool> visited(numrooms, false); | |
bool res = false; | |
visited = dfs(rooms, 0, visited); | |
for(int i = 0; i < numrooms; i++) { | |
if (!visited[i]) return false; | |
} | |
return true; | |
} | |
vector<bool> dfs(vector<vector<int>>& rooms, int room, vector<bool> visited) { | |
visited[room] = true; | |
for(int nextroom : rooms[room]) { | |
if (visited[nextroom] == false) visited = dfs(rooms, nextroom, visited); | |
} | |
return visited; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment