問題
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; };