mirror of
https://github.com/heqin-zhu/algorithm.git
synced 2024-03-22 13:30:46 +08:00
15 lines
418 B
Python
15 lines
418 B
Python
|
class LRUCache(object):
|
||
|
|
||
|
def __init__(self, capacity):
|
||
|
self.od, self.cap = collections.OrderedDict(), capacity
|
||
|
|
||
|
def get(self, key):
|
||
|
if key not in self.od: return -1
|
||
|
self.od.move_to_end(key)
|
||
|
return self.od[key]
|
||
|
|
||
|
def put(self, key, value):
|
||
|
if key in self.od: del self.od[key]
|
||
|
elif len(self.od) == self.cap: self.od.popitem(False)
|
||
|
self.od[key] = value
|