concise and short c++ solution 89ms


  • 0
    E
    int findLHS(vector<int>& nums) {
    	if (nums.empty()) return 0;
    
    	int result = 0;
    	unordered_map<int, int> mp;
    
    	for (const auto& n : nums) {
    		++mp[n];
    	}
    
    	for (auto i = mp.begin(); i != mp.end(); ++i) {
    		if (mp.find(i->first + 1) != mp.end())
    			result = max(result, i->second + mp[i->first + 1]);
    	}
    
    	return result;
    }
    

  • 0
    W

    Thx for your answer.
    It's quite awesome!!!


Log in to reply
 

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