-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray532.java
More file actions
39 lines (36 loc) · 910 Bytes
/
Array532.java
File metadata and controls
39 lines (36 loc) · 910 Bytes
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
36
37
38
39
package array;
import java.util.HashMap;
import java.util.Map;
/**
* @ProjectName: leetcode
* @Package: array
* @ClassName: Array532
* @Author: markey
* @Description:
* @Date: 2020/2/8 23:06
* @Version: 1.0
*/
public class Array532 {
public int findPairs(int[] nums, int k) {
if (k < 0) {
return 0;
}
Map<Integer, Integer> map = new HashMap<>();
for (int i: nums) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
int res = 0;
for (int key: map.keySet()) {
if (k == 0) {
res += map.get(key) / 2;
} else {
if (map.containsKey(key + k)) {
int min = Math.min(map.get(key), map.get(key + k));
res += min;
map.put(key, map.get(key) - min);
}
}
}
return res;
}
}