concise c++ solution


  • 0
    T
    int findLHS(vector<int>& nums) {
        int size = nums.size();
        if(size == 0){
            return 0;
        }
        int length = 0;
        multiset<int> order(nums.begin(), nums.end());
        int number1 = 0, number2 = 0;
        int last = *(order.begin());
        multiset<int> ::iterator it = order.begin();
        for(++it; it!=order.end();){
            if(*it - last == 1){
                number2 = order.count(*it);
                number1 = order.count(last);
                if(length < (number1+number2)){
                    length = number1+number2;
                }
                
            }
            last = *it;
            ++it;
        }
        
        return length;
    }

Log in to reply
 

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