Skip to content

Commit 8ba7179

Browse files
refactor 532
1 parent c654d01 commit 8ba7179

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/main/java/com/fishercoder/solutions/_532.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,31 @@ The pairs (i, j) and (j, i) count as the same pair.
3232
*/
3333
public class _532 {
3434

35-
public int findPairs(int[] nums, int k) {
36-
if (nums == null || nums.length == 0 || k < 0) {
37-
return 0;
38-
}
35+
public static class Solution1 {
36+
public int findPairs(int[] nums, int k) {
37+
if (nums == null || nums.length == 0 || k < 0) {
38+
return 0;
39+
}
3940

40-
Map<Integer, Integer> map = new HashMap();
41-
for (int num : nums) {
42-
map.put(num, map.getOrDefault(num, 0) + 1);
43-
}
41+
Map<Integer, Integer> map = new HashMap();
42+
for (int num : nums) {
43+
map.put(num, map.getOrDefault(num, 0) + 1);
44+
}
4445

45-
int answer = 0;
46-
for (int key : map.keySet()) {
47-
if (k == 0) {
48-
if (map.get(key) >= 2) {
49-
answer++;
50-
}
51-
} else {
52-
if (map.containsKey(key + k)) {
53-
answer++;
46+
int answer = 0;
47+
for (int key : map.keySet()) {
48+
if (k == 0) {
49+
if (map.get(key) >= 2) {
50+
answer++;
51+
}
52+
} else {
53+
if (map.containsKey(key + k)) {
54+
answer++;
55+
}
5456
}
5557
}
58+
return answer;
5659
}
57-
return answer;
5860
}
5961

6062
}

src/test/java/com/fishercoder/_532Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
import static junit.framework.Assert.assertEquals;
1313

1414
public class _532Test {
15-
private static _532 test;
15+
private static _532.Solution1 test;
1616
private static int expected;
1717
private static int actual;
1818
private static int k;
1919
private static int[] nums;
2020

2121
@BeforeClass
2222
public static void setup() throws IOException {
23-
test = new _532();
23+
test = new _532.Solution1();
2424
Properties properties = new Properties();
2525
InputStream inputStream = _532.class.getClassLoader().getResourceAsStream("fishercoder.properties");
2626
properties.load(inputStream);

0 commit comments

Comments
 (0)