lurenaa的博客

DFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
bool canVisitAllRooms(vector<vector<int>>& rooms) {
stack<int > stk;
set<int> st;
stk.push(0);
st.insert(0);
while(stk.size()) {
int pos = stk.top();
stk.pop();
for(auto key : rooms[pos]){
if(!st.count(key)){
st.insert(key);
stk.push(key);
}
}
}
return st.size() == rooms.size();
}
};

Accepted

67/67 cases passed (12 ms)

Your runtime beats 83.56 % of cpp submissions

Your memory usage beats 21.64 % of cpp submissions (11.2 MB)