问题
ide
Design and implement a data structure for Least Recently Used (LRU) cache.
this
It should support the following operations: get and set.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.ci
分析get
为了使查找,插入,删除都高效,须要使用一个哈希表(std::unordered_map)+一个双向链表(std::list)it
哈希表用来保存每一个list节点的地址(不是单纯的value值),能够基本保证在O(1)时间内查找到节点io
双向链表插入删除效率高,之因此不使用单链表是由于插入删除时还须要知道其前驱节点不方便ast
具体实现:越靠近链表头部说明数据越新。尾部数听说明最近访问不多class
当访问节点时,先从哈希表找到该节点的地址:效率
若不存在,则进行插入操做,先判断capacity上限是否已满,若满了就把链表尾部的节点删去,并把该节点在哈希表对应的项也同时删去。插入时把要插入的数据插到链表的头部sed
若存在,则把对应的节点交换到链表的头部,同时更新哈希表中该节点的地址
class LRUCache{ private: struct CacheNode { int key; int value; CacheNode(int k, int v) :key(k), value(v){} }; public: LRUCache(int capacity) { this->capacity = capacity; } int get(int key) { if (cacheMap.find(key) == cacheMap.end()) return -1; cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]); cacheMap[key] = cacheList.begin(); return cacheMap[key]->value; } void set(int key, int value) { if (cacheMap.find(key) == cacheMap.end()) { if (cacheList.size() == capacity) { cacheMap.erase(cacheList.back().key); cacheList.pop_back(); } cacheList.push_front(CacheNode(key, value)); cacheMap[key] = cacheList.begin(); } else { cacheMap[key]->value = value; cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]); cacheMap[key] = cacheList.begin(); } } private: list<CacheNode> cacheList; unordered_map<int, list<CacheNode>::iterator> cacheMap; int capacity; };