LeetCode | LRU Cache
来源:互联网 发布:集美大学网络 编辑:程序博客网 时间:2024/04/29 23:19
题目:
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.
思路:
问题的关键是实现一个优先队列。由于数组的时间开销太大,我选择了链表以及数列来实现这个优先队列。当然,如果需要更好的性能,可以考虑使用堆栈来实现。
代码:
链表版本
class ListNode1{public: int key; int value; ListNode1* next; ListNode1(int key, int value) { this->key = key; this->value = value; next = NULL; }};class LRUCache{public: ListNode1 * begin; int len; LRUCache(int capacity) { begin = NULL; len = capacity; } int get(int key) { if(begin==NULL) return -1; if(begin->key == key) { return begin->value; } else { ListNode1* p = begin; int cur = 1; while(p->next != NULL&&cur < len) { cur++; if(p->next->key == key) { ListNode1* tmp = p->next; p->next = p->next->next; tmp->next = begin; begin = tmp; return begin->value; } p=p->next; }; return -1; } } void set(int key, int value) { if(begin == NULL) begin =new ListNode1(key,value); else { ListNode1* p = new ListNode1(key,value); p->next= begin; begin=p; int cur = 1; while(p->next != NULL&&cur < len) { cur++; if(p->next->key == key) { p->next = p->next->next; cur--; continue; } p=p->next; }; p->next=NULL; } }};
数列版本
class KeyValuePair{public: int key; int value;};class LRUCache{public: KeyValuePair* cache; int length; int capacity; LRUCache(int capacity) { cache = new KeyValuePair[capacity]; length = 0; this->capacity = capacity; } int get(int key) { int value = searchAndGetValue(key); if(value != -1){ insert(key, value); } return value; } void set(int key, int value) { searchAndGetValue(key); if(length == capacity){ remove(0); } insert(key, value); } int searchAndGetValue(int key){ int result = -1; for(int i = 0; i < length; i++){ if(cache[i].key == key){ result = cache[i].value; remove(i); break; } } return result; } void insert(int key, int value){ KeyValuePair tmp; tmp.key = key; tmp.value = value; cache[length++] = tmp; } void remove(int index){ for(int i = index; i < length - 1; i++){ cache[i] = cache[i+1]; } length--; }};
0 0
- 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
- [Leetcode] LRU Cache (Java)
- 设置QPushButton鼠标指向时变成手形
- 跨平台的一些开源框架
- qextserialport 1.1 串口编译
- fw 2013年12月14日12:51:42 selection_sort
- java.lang.ClassCastException异常处理
- LeetCode | LRU Cache
- Ubuntu 网站服务器环境搭建
- Qt串口通信专题教程
- Tomcat映射外部资源,部署应用
- EF直接更新数据(不需查询)
- 《黑马程序员》 鼠标和键盘监听器 窗体事件 图形化管理界面 day22
- Debian ibus配置
- 我的WCF4 Rest Service及Entity Framework with POCO之旅(一)——创建一个基本的RESTful Service
- Debian安装的Chromium-browser中文显示不正常解决方法