mirror of
https://github.com/heqin-zhu/algorithm.git
synced 2024-03-22 13:30:46 +08:00
26 lines
770 B
Python
26 lines
770 B
Python
''' mbinary
|
|
#########################################################################
|
|
# File : lru_orderedDict.py
|
|
# Author: mbinary
|
|
# Mail: zhuheqin1@gmail.com
|
|
# Blog: https://mbinary.xyz
|
|
# Github: https://github.com/mbinary
|
|
# Created Time: 2019-05-23 20:13
|
|
# Description:
|
|
#########################################################################
|
|
'''
|
|
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
|