forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_532.java
35 lines (29 loc) · 871 Bytes
/
_532.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
public class _532 {
public static class Solution1 {
public int findPairs(int[] nums, int k) {
if (nums == null || nums.length == 0 || k < 0) {
return 0;
}
Map<Integer, Integer> map = new HashMap();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
int answer = 0;
for (int key : map.keySet()) {
if (k == 0) {
if (map.get(key) >= 2) {
answer++;
}
} else {
if (map.containsKey(key + k)) {
answer++;
}
}
}
return answer;
}
}
}