8 lines c++ solution with only one loop;


  • 0
        int findPairs(vector<int>& nums, int k) {
            if(k<0)return 0;
            unordered_map<int,int> map;
            int res = 0;
            for(int& num:nums){
                map[num]++;
                res += (k==0)?map[num]==2:(map[num]==1)?(map[num+k]>0) + (map[num-k]>0):0;
            }
            return res;
        }
    

Log in to reply
 

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