这次比较有成就感,排序之后就没什么了
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if(nums.empty()){
return 0;
}
sort(nums.begin(),nums.end());
int result = 0;
int temp = 0;
for(int i =0 ;i < nums.size()-1; i++){
if(nums[i+1]-nums[i]==1){
temp++;
}
else if(nums[i+1]-nums[i] == 0){
continue;
}
else{
result = max(temp+1,result);
temp = 0;
}
}
result = max(temp+1,result);
return result;
}
};