class LRUCache: # @param capacity, an integer def __init__(self, capacity): LRUCache.dic=collections.OrderedDict() LRUCache.capacity=capacity LRUCache.size=0 # @return an integer def get(self, key): try: value=LRUCache.dic[key] del LRUCache.dic[key] LRUCache.dic[key]=value return value except KeyError: return -1 # @param key, an integer # @param value, an integer # @return nothing def set(self, key, value): try: del LRUCache.dic[key] LRUCache.dic[key]=value except: if LRUCache.size==LRUCache.capacity: LRUCache.dic.popitem(False) LRUCache.dic[key]=value else: LRUCache.size+=1 LRUCache.dic[key]=value
Friday, September 19, 2014
Leetcode: LRU Cache @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment