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
68 lines (57 loc) · 1.57 KB
/
Copy pathSample.java
File metadata and controls
68 lines (57 loc) · 1.57 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
import java.util.LinkedList;
class MyHashSet {
class Entry{
public int key;
public Entry(int key){
this.key = key;
}
}
LinkedList<Entry>[] set;
public static int size = 769;
public MyHashSet() {
set = new LinkedList[size];
}
public void add(int key) {
int bucket = (key % size);
if (set[bucket] == null) set[bucket] = new LinkedList<>();
for (Entry e : set[bucket]) {
if (e.key == key) return;
}
set[bucket].addLast(new Entry(key));
}
public void remove(int key) {
int bucket = (key % size);
if (set[bucket] != null) {
Entry toRemove=null;
for (Entry e : set[bucket]) {
if (e.key == key)
{
toRemove=e;
break;
}
}
if(toRemove!=null) set[bucket].remove(toRemove);
}
}
public boolean contains(int key) {
int bucket = (key % size);
if (set[bucket] != null) {
for (Entry e : set[bucket]) {
if (e.key == key) return true;
}
}
return false;
}
}
/**
* 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);
*/