My solution with c++


  • 0
    S
    int longestConsecutive(vector<int>& nums) {
        unordered_map<int,int> counter;
        unordered_map<int,bool> visit;
        unordered_map<int,bool> exist;
        int res=0;
        int i,j,k;
        for(i=0;i<nums.size();i++){
            exist[nums[i]]=true;
        }
        
        for(i=0;i<nums.size();i++){
            int n=nums[i];
            if(visit[n])
                continue;
            int cc=0;
            while(exist[n]){
                if(counter[n]){
                    cc+=counter[n];
                    break;
                }
                visit[n]=true;
                cc++;
                n--;
            }
            counter[nums[i]]=cc;
            res=res<cc?cc:res;
        }
        return res;
    }

Log in to reply
 

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