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
frog-jump.cpp 834 Bytes
Copy Edit Raw Blame History
kamyu authored 2016-09-24 22:17 +08:00 . Update frog-jump.cpp
// Time: O(n) ~ O(n^2)
// Space: O(n)
class Solution {
public:
bool canCross(vector<int>& stones) {
if (stones[1] != 1) {
return false;
}
unordered_map<int, unordered_set<int>> last_jump_units;
for (const auto& s: stones) {
last_jump_units.emplace(s, {unordered_set<int>()});
}
last_jump_units[1].emplace(1);
for (int i = 0; i + 1 < stones.size(); ++i) {
for (const auto& j : last_jump_units[stones[i]]) {
for (const auto& k : {j - 1, j, j + 1}) {
if (k > 0 && last_jump_units.count(stones[i] + k)) {
last_jump_units[stones[i] + k].emplace(k);
}
}
}
}
return !last_jump_units[stones.back()].empty();
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search