Skip to content

Commit 7fdadb1

Browse files
refactor 454
1 parent cba0670 commit 7fdadb1

File tree

2 files changed

+31
-37
lines changed

2 files changed

+31
-37
lines changed

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

+28-22
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6-
/**Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.
7-
8-
To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.
6+
/**
7+
* 454. 4Sum II
8+
*
9+
* Given four lists A, B, C, D of integer values,
10+
* compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.
11+
* To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500.
12+
* All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.
913
1014
Example:
1115
@@ -25,30 +29,32 @@
2529

2630
public class _454 {
2731

28-
public static int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
29-
Map<Integer, Integer> map = new HashMap();
30-
int result = 0;
31-
int len = A.length;
32-
for (int i = 0; i < len; i++) {
33-
for (int j = 0; j < len; j++) {
34-
int sum = A[i] + B[j];
35-
if (map.containsKey(sum)) {
36-
map.put(sum, map.get(sum) + 1);
37-
} else {
38-
map.put(sum, 1);
32+
public static class Solution1 {
33+
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
34+
Map<Integer, Integer> map = new HashMap();
35+
int result = 0;
36+
int len = A.length;
37+
for (int i = 0; i < len; i++) {
38+
for (int j = 0; j < len; j++) {
39+
int sum = A[i] + B[j];
40+
if (map.containsKey(sum)) {
41+
map.put(sum, map.get(sum) + 1);
42+
} else {
43+
map.put(sum, 1);
44+
}
3945
}
4046
}
41-
}
4247

43-
for (int i = 0; i < len; i++) {
44-
for (int j = 0; j < len; j++) {
45-
int sum = -(C[i] + D[j]);
46-
if (map.containsKey(sum)) {
47-
result += map.get(sum);
48+
for (int i = 0; i < len; i++) {
49+
for (int j = 0; j < len; j++) {
50+
int sum = -(C[i] + D[j]);
51+
if (map.containsKey(sum)) {
52+
result += map.get(sum);
53+
}
4854
}
4955
}
50-
}
5156

52-
return result;
57+
return result;
58+
}
5359
}
5460
}

src/test/java/com/fishercoder/_454Test.java

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._454;
4-
import org.junit.Before;
54
import org.junit.BeforeClass;
65
import org.junit.Test;
76

87
import static junit.framework.Assert.assertEquals;
98

109
public class _454Test {
11-
private static _454 test;
10+
private static _454.Solution1 solution1;
1211
private static int expected;
1312
private static int actual;
1413
private static int[] A;
@@ -18,28 +17,17 @@ public class _454Test {
1817

1918
@BeforeClass
2019
public static void setup() {
21-
test = new _454();
22-
}
23-
24-
@Before
25-
public void setupForEachTest() {
26-
expected = 0;
27-
actual = 0;
28-
A = new int[1000];
29-
B = new int[1000];
30-
C = new int[1000];
31-
D = new int[1000];
20+
solution1 = new _454.Solution1();
3221
}
3322

3423
@Test
3524
public void test1() {
36-
3725
A = new int[]{1, 2};
3826
B = new int[]{-2, -1};
3927
C = new int[]{-1, 2};
4028
D = new int[]{0, 2};
4129
expected = 2;
42-
actual = test.fourSumCount(A, B, C, D);
30+
actual = solution1.fourSumCount(A, B, C, D);
4331
assertEquals(expected, actual);
4432

4533
}

0 commit comments

Comments
 (0)