We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a4ac7bb commit 360a9e1Copy full SHA for 360a9e1
LeetCode/532.k-diff-pairs-in-an-array.cpp
@@ -0,0 +1,21 @@
1
+using hashmap = unordered_map<int, int>;
2
+
3
+class Solution {
4
+public:
5
+ int findPairs(vector<int>& nums, int k) {
6
+ hashmap cnt;
7
+ for(int x: nums) cnt[x]++;
8
9
+ int ans = 0;
10
+ for(auto p: cnt) { // iterating on unique numbers of the array
11
+ int x = p.first;
12
+ // check x+k exists in the array
13
+ if(cnt.find(x+k) == cnt.end()) {
14
+ continue;
15
+ }
16
+ ans += (k==0) ? cnt[x+k] >= 2 : cnt[x+k] >= 1;
17
18
19
+ return ans;
20
21
+};
0 commit comments