Leetcode 128 *

时间:2022-04-04 18:02:03
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int res = ;
unordered_map<int,int> m;
for(int i=;i < nums.size();i++){
if(m.count(nums[i])) continue;
int left = (m.count(nums[i]-) > ? m[nums[i]-]:);
int right = (m.count(nums[i]+) > ? m[nums[i]+]:);
int sum = left + right + ;
m[nums[i]] = sum; // 为什么要加这个,不是改变两端么
res = max(res,sum);
m[nums[i]-left] = sum;
m[nums[i]+right] = sum;
}
return res;
}
};

还有bug,太晚了不想想了,下次再补