https://leetcode.cn/problems/longest-consecutive-sequence/description/?envType=study-plan-v2&envId=top-100-liked

#include <vector>
#include <unordered_set>
#include <algorithm>
class Solution {
public:
int longestConsecutive(std::vector<int>& nums) {
std::unordered_set<int> num_set;
for (const auto& num : nums) {
num_set.insert(num);
}
int ret = 0;
for (const auto& num : num_set) {
if (!num_set.count(num - 1)) {
int cur_num = num;
int cur_len = 1;
while (num_set.count(cur_num + 1)) {
cur_num++;
cur_len++;
}
ret = std::max(ret, cur_len);
}
}
return ret;
}
};