forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHashSet.java
More file actions
56 lines (43 loc) · 1.6 KB
/
MyHashSet.java
File metadata and controls
56 lines (43 loc) · 1.6 KB
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
class MyHashSet {
int primaryArraySize;
int secondaryArraySize;
boolean[][] customHashArray;
public MyHashSet() {
this.primaryArraySize = 1000;
this.secondaryArraySize = 1000;
this.customHashArray = new boolean[primaryArraySize][];
}
public int getPrimaryHashValue(int key) {
return key % primaryArraySize;
}
public int getSecondaryHashValue(int key) {
return key / secondaryArraySize;
}
public void add(int key) {
int primaryPos = getPrimaryHashValue(key);
int secondaryPos = getSecondaryHashValue(key);
if(customHashArray[primaryPos] == null) {
if(primaryPos == 0 && secondaryPos == 1000) {
customHashArray[primaryPos] = new boolean[secondaryArraySize + 1];
} else {
customHashArray[primaryPos] = new boolean[secondaryArraySize];
}
}
customHashArray[primaryPos][secondaryPos] = true;
}
public void remove(int key) {
int primaryPos = getPrimaryHashValue(key);
int secondaryPos = getSecondaryHashValue(key);
if(customHashArray[primaryPos] != null) {
customHashArray[primaryPos][secondaryPos] = false;
}
}
public boolean contains(int key) {
int primaryPos = getPrimaryHashValue(key);
if(customHashArray[primaryPos] == null) {
return false;
}
int secondaryPos = getSecondaryHashValue(key);
return customHashArray[primaryPos][secondaryPos];
}
}