代码
/*
* @lc app=leetcode.cn id=128 lang=cpp
*
* [128] 最长连续序列
*/
// @lc code=start
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> S;
for (auto x : nums) S.insert(x);
int ans = 0;
for (auto x: nums) {
if (S.count(x) && !S.count(x - 1)) {
int y = x;
S.erase(x);
while (S.count(y + 1)) {
y++;
S.erase(y);
}
ans = max(ans, y - x + 1);
}
}
return ans;
}
};
// @lc code=end