示例代码
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> mySet;
for(int num:nums){
mySet.insert(num);
}
int res=0,count=1,temp=0;
for(auto num:mySet){
temp=num;
count=1;
//如果没有比当前数字小1的数字时候再进行计数
if(!mySet.count(num-1)){
while(mySet.count(++temp)){
count++;
}
}
//如果此序列计数的值比结果大,则进行结果值的更新
if(count>res){
res=count;
}
}
return res;
}
};