描述:
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。
获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
写入数据 put(key, value) - 如果密钥已经存在,则变更其数据值;如果密钥不存在,则插入该组「密钥/数据值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。
**顺序之分 区分最近使用和久未使用的数据
法一:
OrderedDict 方法 :有序字典
from collections import OrderedDict
class LRUCache:
def __init__(self, capacity: int):
self.maxsize = capacity
self.lrucache = OrderedDict()
def get(self, key: int) -> int:
if key in self.lrucache:
self.lrucache.move_to_end(key)
return self.lrucache.get(key,-1)
def put(self, key: int, value: int) -> None:
if key in self.lrucache:
del self.lrucache[key]
self.lrucache[key] = value
if len(self.lrucache)>self.maxsize:
self.lrucache.popitem(last = False)
法二:哈希表+双向链表
LRU 缓存算法的核心数据结构就是哈希链表,双向链表和哈希表的结合体
哈希表查找快,但是数据无固定顺序;链表有顺序之分,插入删除快,但是查找慢