Created
September 4, 2019 02:35
-
-
Save lihuanshuai/e0abecf2f2c826bf993bad2589b4bfca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import OrderedDict | |
class LRUCache(object): | |
def __init__(self, size=5): | |
self.size = size | |
self.cache = OrderedDict() | |
def get(self, key): | |
if key in self.cache: | |
val = self.cache.pop(key) | |
self.cache[key] = val | |
else: | |
val = None | |
return val | |
def set(self, key, val): | |
if key in self.cache: | |
val = self.cache.pop(key) | |
self.cache[key] = val | |
else: | |
if len(self.cache) == self.size: | |
self.cache.popitem(last=False) | |
self.cache[key] = val | |
else: | |
self.cache[key] = val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment