https://leetcode.com/problems/k-diff-pairs-in-an-array/description/
int findPairs(vector<int>& nums, int k) {
if(k<0) return 0;
unordered_map<int,int> mp;
int res = 0;
for(auto n: nums) mp[n]++;
if(k==0) {
for(auto &x : mp) {
res += (x.second>1);
}
return res;
}
for(auto &x : mp) {
if(mp.count(x.first+k)) res++;
}
return res;
}
No comments:
Post a Comment