class Solution { public: int longestConsecutive(vector<int>& nums) { int res = 0; unordered_map<int,int> m; for(int i=0;i < nums.size();i++){ if(m.count(nums[i])) continue; int left = (m.count(nums[i]-1) > 0 ? m[nums[i]-1]:0); int right = (m.count(nums[i]+1) > 0 ? m[nums[i]+1]:0); int sum = left + right + 1; m[nums[i]] = sum; // 为什么要加这个,不是改变两端么 res = max(res,sum); m[nums[i]-left] = sum; m[nums[i]+right] = sum; } return res; } };
还有bug,太晚了不想想了,下次再补