-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlru_cache.py
More file actions
30 lines (22 loc) · 726 Bytes
/
Copy pathlru_cache.py
File metadata and controls
30 lines (22 loc) · 726 Bytes
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
class LRUCache:
# @param capacity, an integer
def __init__(self, capacity):
#import collections
self.capacity = capacity
self.data = collections.OrderedDict()
# @return an integer
def get(self, key):
if not key in self.data:
return -1
value = self.data.pop(key)
self.data[key] = value
return value
# @param key, an integer
# @param value, an integer
# @return nothing
def set(self, key, value):
if key in self.data:
self.data.pop(key)
elif len(self.data) == self.capacity:
self.data.popitem(last=False)
self.data[key] = value