a c++ code problem


  • 0
    C

    i think my code should be correct.

    class Solution {
    public:
        int findLHS(vector<int>& nums) {
            unordered_map<int,int> data;
            for (int i = 0; i < nums.size(); ++i)
                data[nums[i]]++;
            int ans = INT_MIN;
            for(auto a:data){
                if(data.count(a.first+1)){
                    ans = max(ans,a.second+data[a.first+1]);
                }
                else{
                    ans = max(ans,a.second);
                }
            }
            return ans;
        }
    };
    

    there a test case [1,1,1,1].
    in my computer, it returns 4, but in leetcode, it return 0.


Log in to reply
 

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