-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0706_design_hashmap.go
More file actions
98 lines (81 loc) · 1.91 KB
/
s0706_design_hashmap.go
File metadata and controls
98 lines (81 loc) · 1.91 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
https://leetcode.com/problems/design-hashmap/
Design a HashMap without using any built-in hash table libraries.
Implement the MyHashMap class:
MyHashMap() initializes the object with an empty map.
void put(int key, int value) inserts a (key, value) pair into the HashMap. If
the key already
exists in the map, update the corresponding value.
int get(int key) returns the value to which the specified key is mapped, or -1
if this map contains
no mapping for the key.
void remove(key) removes the key and its corresponding value if the map
contains the mapping for the key.
*/
//nolint:revive //it's ok
package solutions
const SPACE = 2069
type Pair struct {
k, v int
}
type Bucket struct {
b []Pair
}
func (b Bucket) Get(key int) int {
for _, p := range b.b {
if p.k == key {
return p.v
}
}
return -1
}
func (b *Bucket) Update(key, value int) {
found := false
for i, p := range b.b {
if key == p.k {
b.b[i] = Pair{key, value}
found = true
break
}
}
if !found {
b.b = append(b.b, Pair{key, value})
}
}
func (b *Bucket) Remove(key int) {
for i, p := range b.b {
if p.k == key {
b.b = append(b.b[:i], b.b[i+1:]...)
}
}
}
type MyHashMap struct {
keySpace int
hashTable []Bucket
}
// NewMyHashMap is Constructor for MyHashMap (it must call Constructor for LeetCode)
func NewMyHashMap() MyHashMap {
return MyHashMap{
keySpace: SPACE,
hashTable: make([]Bucket, SPACE),
}
}
func (m *MyHashMap) Put(key, value int) {
hashKey := key % m.keySpace
m.hashTable[hashKey].Update(key, value)
}
func (m MyHashMap) Get(key int) int {
hashKey := key % m.keySpace
return m.hashTable[hashKey].Get(key)
}
func (m *MyHashMap) Remove(key int) {
hashKey := key % m.keySpace
m.hashTable[hashKey].Remove(key)
}
/**
* Your MyHashMap object will be instantiated and called as such:
* obj := Constructor();
* obj.Put(key,value);
* param_2 := obj.Get(key);
* obj.Remove(key);
*/