9 ms C++ using set, concise and clean


  • 0
    T
    class Solution {
    public:
        int longestConsecutive(vector<int>& nums) {
            set<int> s(nums.begin(),nums.end());
            int ans = 0, last = *s.begin()-1, len = 0;
            for(auto i:s)
            {
                if (i-last == 1) ans = max(ans,++len);
                else len = 1;
                last = i;
            }
            return ans;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.