-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesignHashMap.py
More file actions
51 lines (40 loc) · 1.08 KB
/
designHashMap.py
File metadata and controls
51 lines (40 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
49
50
51
class MyHashMap(object):
def __init__(self):
self.capacity = 10000001;
self.set = [None]*self.capacity;
def hash(self, key):
return key % self.capacity;
def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: None
"""
address = hash(key);
if self.set[address] is None:
self.set[address] = [key, value];
else:
self.set[address][1] = value;
def get(self, key):
"""
:type key: int
:rtype: int
"""
address = hash(key);
if self.set[address] is not None:
return self.set[address][1];
else:
return -1;
def remove(self, key):
"""
:type key: int
:rtype: None
"""
address = hash(key);
if self.set[address] is not None:
self.set[address] = None;
# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key