C++


  • 0
    B
    class Solution {
    public:
        int findLHS(vector<int>& nums) {
            unordered_map<int, int> cnt;
            for (auto n : nums) {
                cnt[n] += 1;
            }
            int retVal = 0;
            for (auto v : cnt) {
                if (cnt.find(v.first+1) != cnt.end()) {
                    retVal = max(retVal, cnt[v.first+1]+v.second);
                }
            }
            return retVal;
        }
    };

Log in to reply
 

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