128_LongestConsecutiveSequence - a920604a/leetcode GitHub Wiki


title: 128. Longest Consecutive Sequence tags:
- hash table categories: leetcode comments: false

solution

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        
        // hash table η΄€ιŒ„ζ•Έε­—θˆ‡εŒ…ε«θ©²ζ•Έε­—ηš„ζœ€ι•·ε€ι–“ι•·εΊ¦
        unordered_map<int,int> mp;
        int ret = 0;
        for(int n :nums){
            if(mp.find(n)!=mp.end()) continue;
            int l = (mp.count(n-1))?mp[n-1]:0;
            int r = (mp.count(n+1))?mp[n+1]:0;
            int sum = r+l+1;
            mp[n] = sum;
            mp[n-l] = sum;
            mp[n+r] = sum;
            ret = max(ret,sum);
        }
        return ret;
    }
};

analysis

  • time complexity O(nlogn)
  • space complexity O(n)
⚠️ **GitHub.com Fallback** ⚠️