-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathMyHashSet.java
More file actions
68 lines (45 loc) · 1.51 KB
/
Copy pathMyHashSet.java
File metadata and controls
68 lines (45 loc) · 1.51 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
57
58
59
60
61
62
63
64
65
66
67
68
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
// Your code here along with comments explaining your approach
class MyHashSet {
private boolean storage[][];
private int arraySize1;
private int arraySize2;
public MyHashSet() {
this.arraySize1 = 1000;
this.arraySize2 = 1000;
this.storage = new boolean[arraySize1][];
}
private int hash1(int key){
return key%arraySize1;
}
private int hash2(int key){
return key/arraySize2;
}
public void add(int key) {
int arrayIndex1 = hash1(key);
int arrayIndex2 = hash2(key);
if(storage[arrayIndex1] == null){
if(arrayIndex1 == 0){
storage[arrayIndex1]= new boolean[arraySize2+1];
}else{
storage[arrayIndex1]= new boolean[arraySize2];
}
}
storage[arrayIndex1][arrayIndex2] = true;
}
public void remove(int key) {
int arrayIndex1 = hash1(key);
int arrayIndex2 = hash2(key);
if( storage[arrayIndex1] == null) return;
storage[arrayIndex1][arrayIndex2] = false;
}
public boolean contains(int key) {
int arrayIndex1 = hash1(key);
int arrayIndex2 = hash2(key);
if(storage[arrayIndex1] == null) return false;
return storage[arrayIndex1][arrayIndex2];
}
}