用到了unordered_map
class Solution {
public:
unordered_map<int, int> A;
int longestConsecutive(vector<int>& nums) {
int ans = 0;
for (auto &x : nums)
if (!A.count(x)) {//x未出现过
int L = A.count(x - 1) ? A[x - 1] : 0,
R = A.count(x + 1) ? A[x + 1] : 0,
n = L + R + 1;
A[x] = A[x - L] = A[x + R] = n;
ans = max(ans, n);
}
return ans;
}
};