Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
keys-and-rooms.cpp 667 Bytes
Copy Edit Raw Blame History
kamyu authored 2018-05-28 00:35 +08:00 . Create keys-and-rooms.cpp
// Time: O(n!)
// Space: O(n)
class Solution {
public:
bool canVisitAllRooms(vector<vector<int>>& rooms) {
unordered_set<int> lookup = {0};
vector<int> stack = {0};
while (!stack.empty()) {
auto node = stack.back(); stack.pop_back();
for (const auto& nei : rooms[node]) {
if (!lookup.count(nei)) {
lookup.emplace(nei);
if (lookup.size() == rooms.size()) {
return true;
}
stack.emplace_back(nei);
}
}
}
return lookup.size() == rooms.size();
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search