0128.cpp (476B)
1 class Solution { 2 public: 3 int longestConsecutive(vector<int> &nums) { 4 if (!nums.size()) return 0; 5 6 unordered_set<int> us(nums.begin(), nums.end()); 7 int res = 0; 8 9 for (int num : us) { 10 if (!us.count(num - 1)) { 11 int crnt = num; 12 while (us.count(crnt + 1)) 13 crnt += 1; 14 res = max(longestStreak, crnt - num); 15 } 16 } 17 18 return res + 1; 19 } 20 };