-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW02_4111056036_5.java
77 lines (64 loc) · 1.77 KB
/
HW02_4111056036_5.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public class HW02_4111056036_5 extends FourSum {
class Node {
int sum, value;
public Node(int sum) {
this.sum = sum;
this.value = 1;
}
}
class HashTable {
int size;
Node[] nodes;
public HashTable(int size) {
this.nodes = new Node[this.size = size];
}
public int hash(int sum) {
int hashKey = ((sum % size) + size) % size;
while (nodes[hashKey] != null && nodes[hashKey].sum != sum) {
hashKey = (hashKey + 1) % size;
}
return hashKey;
}
public void add(int sum) {
int key = hash(sum);
Node node = nodes[key];
if(node == null) {
nodes[key] = new Node(sum);
return;
}
++nodes[key].value;
}
public int get(int sum) {
int key = hash(sum);
Node node = nodes[key];
return node == null ? 0 : node.value;
}
}
public HW02_4111056036_5() {
}
@Override
public int F_sum(int[] A) {
int ans = 0;
int n = A.length;
HashTable table = new HashTable(25000);
int sum;
java.util.Arrays.sort(A);
for(int i = 0; i < n - 1; ++i) {
for(int j = n - 1; j > i; --j) {
sum = A[i] + A[j];
if(sum < 0) {
break;
}
ans += table.get(sum);
}
for(int j = 0; j < i; ++j) {
sum = A[i] + A[j];
if(sum > 0) {
break;
}
table.add(-sum);
}
}
return ans;
}
}