首页 > 代码库 > LRU Cache
LRU Cache
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.
思路:get函数在Cache查找key的值,如果存在于Cache中,则将该键值移到Cache首位置,并返回值value反之,则返回-1;set(key,value)函数,如果key存在,则更新相应的value把该元素放到最前面。如果不存在,则创建,并放到最前面,如果容器满了,就把最后那个元素去除。从这可以看出,元素访问的先后是有一定的顺序的,我们可以采用map来对元素进行快速查找,然后定位到查找的结点,使用双向链表来进行移动或删除都很方便。这里使用STL中list容器对于移动或删除都比较容易,代码也比较简洁。
struct CacheNode{ int key; int value; CacheNode(int k,int v):key(k),value(v){}};class LRUCache{private: int size; list<CacheNode> cacheList; unordered_map<int,list<CacheNode>::iterator > cacheMap;public: LRUCache(int capacity) { this->size=capacity; } int get(int key) { if(cacheMap.find(key)!=cacheMap.end()) { list<CacheNode>::iterator iter=cacheMap[key]; cacheList.splice(cacheList.begin(),cacheList,iter); cacheMap[key]=cacheList.begin(); return cacheList.begin()->value; } else return -1; } void set(int key, int value) { if(cacheMap.find(key)==cacheMap.end()) { if(cacheList.size()==size) { cacheMap.erase(cacheList.back().key); cacheList.pop_back(); } cacheList.push_front(CacheNode(key,value)); cacheMap[key]=cacheList.begin(); } else { list<CacheNode>::iterator iter=cacheMap[key]; cacheList.splice(cacheList.begin(),cacheList,iter); cacheMap[key]=cacheList.begin(); cacheList.begin()->value=http://www.mamicode.com/value; } }};
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。