LeetCode (26) LRU Cache
来源:互联网 发布:淘宝亲亲节 编辑:程序博客网 时间:2024/06/14 08:22
题目描述
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)和插入(set)。需要注意的是,当缓存满了的时候,在set时需要删除最长时间没有使用的元素,并插入新元素。这就要求我们在数据结构中设计一个方法要么记录每个元素的被使用顺序,要么根据元素的被使用顺序保存元素。需要注意的是,在get元素的时候,如果元素存在于缓存中,则该元素的被使用顺序要被提前。
可以通过使用双向链表表示缓存中数据。链表的头部表示最近被使用的元素,链表的尾部表示最长时间没被使用的元素。当获取/插入元素时,将元素放置在链表的头部。需要注意的是,若插入的元素的key已经存在于缓存中,则要删除原来的数据(key, value)结点。
我们知道对链表进行随机访问的时间复杂度为
map结构如下:
map<int, node*> cache;
根据上述思路得到的完整代码如下:
struct node{ int key; int value; node* pre; node* next; node(int k, int v) :key(k), value(v), pre(NULL), next(NULL) {};};class LRUCache{private: int m_capacity; int m_size; node* m_head; node* m_tail; map<int, node*> m_cache;public: LRUCache(int capacity) { m_capacity = capacity; m_size = 0; m_head = new node(0, 0); m_tail = new node(0, 0); m_head->next = m_tail; m_tail->pre = m_head; m_cache.clear(); } ~LRUCache() { delete m_head; delete m_tail; map<int, node*>::iterator it; for (it = m_cache.begin(); it != m_cache.end(); ++it) { delete(it->second); } m_cache.clear(); } void putTohead(node* cur) { cur->next = m_head->next; cur->next->pre = cur; m_head->next = cur; cur->pre = m_head; } int get(int key) { map<int, node*>::iterator it = m_cache.find(key); if (it != m_cache.end()) { node* cur = it->second; cur->pre->next = cur->next; cur->next->pre = cur->pre; putTohead(cur); return cur->value; } else return -1; } void set(int key, int value) { map<int, node*>::iterator it = m_cache.find(key); if (it != m_cache.end()) { node* cur = it->second; cur->value = value; cur->pre->next = cur->next; cur->next->pre = cur->pre; putTohead(it->second); } else { node* new_node = new node(key, value); putTohead(new_node); m_cache[key] = new_node; if (m_size < m_capacity) m_size++; else { node* del = m_tail->pre; del->pre->next = m_tail; m_tail->pre = del->pre; it = m_cache.find(del->key); m_cache.erase(it); delete del; } } }};
- LeetCode (26) LRU Cache
- LeetCode LRU 缓存 LRU Cache
- LRU Cache | leetcode
- Leetcode: LRU Cache
- LeetCode:LRU Cache
- Leetcode: LRU Cache
- [LeetCode] LRU Cache
- leetcode之LRU Cache
- [LeetCode]LRU Cache
- [LeetCode] LRU Cache
- leetcode LRU Cache
- LeetCode题解:LRU Cache
- LeetCode | LRU Cache
- Leetcode: LRU Cache
- [LeetCode] LRU Cache
- LeetCode OJ:LRU Cache
- Leetcode LRU Cache
- LeetCode - LRU Cache
- 《高性能MySQL》读书笔记--MySQL逻辑架构
- dfs代码
- 使用XFire进行web services开发有三种方式:
- Linux启动顺序
- 11 CI中的【模型】 控制器 视图
- LeetCode (26) LRU Cache
- 360 2016届实习生招聘笔试 综合题
- 不同浏览器上中文文件名的下载乱码问题
- Android“再按一次退出程序”的具体实现
- 第八周项目1(1)-复数类的运算重载
- Linux平台总线驱动设备模型
- 从安装java 到hadoop 到spark 的全套步骤,很详细, 高档大气上档次
- 正则表达式-元字符
- leetcode 日经贴,Cpp code -Letter Combinations of a Phone Number