D_L3

longestConsecutive

Dec 12th, 2023
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int longestConsecutive(vector<int>& nums) {
  4.         unordered_set<int> numsSet;
  5.         for(auto i : nums)
  6.             numsSet.insert(i);
  7.            
  8.         int longest = 0;
  9.         for(auto i : numsSet) {
  10.             int len = 0;
  11.             if(numsSet.count(i - 1) == 0){
  12.                 while(numsSet.count(i + len) != 0)
  13.                     len ++;
  14.                 longest = max(longest, len);
  15.             }
  16.         }
  17.         return longest;
  18.     }
  19. };
Advertisement
Add Comment
Please, Sign In to add comment