Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int longestConsecutive(vector<int>& nums) {
- unordered_set<int> numsSet;
- for(auto i : nums)
- numsSet.insert(i);
- int longest = 0;
- for(auto i : numsSet) {
- int len = 0;
- if(numsSet.count(i - 1) == 0){
- while(numsSet.count(i + len) != 0)
- len ++;
- longest = max(longest, len);
- }
- }
- return longest;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment