C++ Solution


  • 0
    Z
    class Solution {
    public:
        int findLHS(vector<int>& nums) {
            unordered_map<int, int> my_map_a, my_map_b;
            int max_len = 0;
            for(int num : nums) {
                ++my_map_a[(num << 1) + 1];
                if(my_map_b.find((num << 1) + 1) != my_map_b.end()) {
                    max_len = max(max_len, my_map_a[(num << 1) + 1] + my_map_b[(num << 1) + 1]);
                }
                ++my_map_b[(num << 1) - 1];
                if(my_map_a.find((num << 1) - 1) != my_map_a.end()) {
                    max_len = max(max_len, my_map_a[(num << 1) - 1] + my_map_b[(num << 1) - 1]);
                }
            }
            return max_len;
        }
    };
    

Log in to reply
 

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