forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.go
46 lines (40 loc) · 882 Bytes
/
Solution2.go
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
type MyHashSet struct {
data []list.List
}
func Constructor() MyHashSet {
return MyHashSet{make([]list.List, 1000)}
}
func (this *MyHashSet) Add(key int) {
if this.Contains(key) {
return
}
idx := this.hash(key)
this.data[idx].PushBack(key)
}
func (this *MyHashSet) Remove(key int) {
idx := this.hash(key)
for e := this.data[idx].Front(); e != nil; e = e.Next() {
if e.Value.(int) == key {
this.data[idx].Remove(e)
}
}
}
func (this *MyHashSet) Contains(key int) bool {
idx := this.hash(key)
for e := this.data[idx].Front(); e != nil; e = e.Next() {
if e.Value.(int) == key {
return true
}
}
return false
}
func (this *MyHashSet) hash(key int) int {
return key % len(this.data)
}
/**
* Your MyHashSet object will be instantiated and called as such:
* obj := Constructor();
* obj.Add(key);
* obj.Remove(key);
* param_3 := obj.Contains(key);
*/