-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path705.java
41 lines (32 loc) · 1005 Bytes
/
705.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
class MyHashSet {
private int buckets = 1000;
private int itemsPerBucket = 1001;
private boolean[][] table;
/** Initialize your data structure here. */
public MyHashSet() {
table = new boolean[buckets][];
}
public int hash(int key) {
return key % buckets;
}
public int pos(int key) {
return key / buckets;
}
public void add(int key) {
int hashkey = hash(key);
if (table[hashkey] == null) {
table[hashkey] = new boolean[itemsPerBucket];
}
table[hashkey][pos(key)] = true;
}
public void remove(int key) {
int hashkey = hash(key);
if (table[hashkey] != null)
table[hashkey][pos(key)] = false;
}
/** Returns true if this set did not already contain the specified element */
public boolean contains(int key) {
int hashkey = hash(key);
return table[hashkey] != null && table[hashkey][pos(key)];
}
}