-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo532.java
42 lines (38 loc) · 870 Bytes
/
No532.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
36
37
38
39
40
41
42
package Algorithm.leetcode.Array;
import java.util.HashSet;
import java.util.Set;
/**
*
* Created by tujietg on Nov 14, 2019
*/
public class No532 {
public int findPairs(int[] nums, int k) {
Set<Integer> hashSet = new HashSet<Integer>();
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (Math.abs(nums[i] - nums[j]) == k) {
if (nums[i] > nums[j]) {
hashSet.add(nums[j]);
} else {
hashSet.add(nums[i]);
}
}
}
}
return hashSet.size();
}
public static int findPairs02(int[] nums, int k) {
if (k < 0)
return 0;
Set<Integer> numbers = new HashSet<>();
Set<Integer> found = new HashSet<>();
for (int n : nums) {
if (numbers.contains(n + k))
found.add(n);
if (numbers.contains(n - k))
found.add(n);
numbers.add(n - k);
}
return found.size();
}
}