C++ solution


  • 0

    class Solution {
    public:
    int findPairs(vector<int>& nums, int k) {
    std::set<std::pair<int, int>> result;

        for (int i = 0; i < nums.size(); i++)
        {
            for (int j = i + 1; j < nums.size(); j++)
            {
                if (std::abs(nums[i] - nums[j]) == k)
                {
                    int a = std::min(nums[i], nums[j]);
                    int b = std::max(nums[i], nums[j]);
                    result.insert(std::make_pair(a, b));
                }
            } 
        }
    
        return result.size();
    }
    

    };


Log in to reply
 

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