C++_unordered_map AC


  • 0
    class Solution {
    public:
    int findPairs(vector<int>& nums, int k) {
        if(nums.empty() || k < 0) return 0;
        unordered_map<int, int> mp;
        for(auto num : nums){
            mp[num]++;
        }
        int res = 0;
        for(auto num : nums){
            if(mp.find(num) == mp.end()) continue;
            if(k == 0){
                res += mp[num] >= 2 ? 1 : 0;
            }else{
                if(mp.find(num - k) != mp.end()) res++;
                if(mp.find(num + k) != mp.end()) res++;
            }
            mp.erase(num);
        }
        return res;
    }
    };

Log in to reply
 

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