您的位置:首页 > 其它

算法学习--链表/Hash--LRU cache

2017-10-31 21:08 211 查看
问题

Design and implement a data structure for Least Recently Used (LRU) cache.

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.
分析
为了使查找,插入,删除都高效,需要使用一个哈希表(std::unordered_map)+一个双向链表(std::list)
哈希表用来保存每个list节点的地址(不是单纯的value值),可以基本保证在O(1)时间内查找到节点
双向链表插入删除效率高,之所以不使用单链表是因为插入删除时还需要知道其前驱节点不方便
具体实现:越靠近链表头部说明数据越新。尾部数据说明最近访问很少
当访问节点时,先从哈希表找到该节点的地址:
若不存在,则进行插入操作,先判断capacity上限是否已满,若满了就把链表尾部的节点删去,并把该节点在哈希表对应的项也同时删去。插入时把要插入的数据插到链表的头部
若存在,则把对应的节点交换到链表的头部,同时更新哈希表中该节点的地址
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;
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 链表 哈希表