My Short C++ solution using unordered map,so it's O(n) time and O(n) space.


  • 6
    H
    class Solution {
    public:
    int longestConsecutive(vector<int>& nums) {
        unordered_map<int,bool> mp;
        int ret = 0;
        for(const auto x:nums)
            mp[x] = true;
        for(const auto x : mp)
            if(x.second)
            {
                int left = x.first,right = x.first+1;
                while(mp.count(left) && mp[left])
                    mp[left--] = false;
                while(mp.count(right) && mp[right])
                    mp[right++] = false;
                ret = max(ret,right - left  - 1);
            }
        return ret;
    }
    };

  • 0
    This post is deleted!

Log in to reply
 

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