Simple C++ 8 lines hash table solution


  • 0
        int findLHS(vector<int>& nums) {
            int max_len=0;
            unordered_map<int,int>m;
            for(int i=0;i<nums.size();i++) m[nums[i]]++;
            for(auto x:m){
                int count=m.count(x.first-1) ? x.second+m[x.first-1] : 0;
                if(count>max_len) max_len=count;
            }
            return max_len;
        }
    

Log in to reply
 

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