classSolution{public:intlongestConsecutive(vector<int>& nums){
unordered_set<int> hash;for(constint& num : nums)
hash.insert(num);//将nums中数遍历加入至哈希表中int ans =0;while(!hash.empty()){int cur =*(hash.begin());
hash.erase(cur);int next = cur +1;int pre = cur -1;while(hash.count(next))//当前数的右侧可连续,继续排除
hash.erase(next++);while(hash.count(pre))//当前数的左侧可连续,继续排除
hash.erase(pre--);
ans =max(ans, next - pre -1);//比较当前最大连续长度}return ans;}};