forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.java
More file actions
48 lines (35 loc) · 1.08 KB
/
Copy pathSample.java
File metadata and controls
48 lines (35 loc) · 1.08 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
// Time Complexity : O(1)
// Space Complexity : O(N)
// Did this code successfully run on LeetCode: yes
// Any problem you faced while coding this : no
// Your code here along with comments explaining your approach
class MyHashSet {
private static final int SIZE = 420;
private LinkedList<Integer>[] buckets;
public MyHashSet() {
buckets = new LinkedList[SIZE];
for( int i = 0; i<SIZE; i++){
buckets[i] = new LinkedList<Integer>();
}
}
private int hash(int key) {
return key % SIZE;
}
public void add(int key) {
LinkedList<Integer> bucket = buckets[hash(key)];
if(!bucket.contains(key)) bucket.add(key);
}
public void remove(int key) {
buckets[hash(key)].remove((Integer)key);
}
public boolean contains(int key) {
return buckets[hash(key)].contains(key);
}
}
/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/