【Leetcode】 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.
采用单链表,并通过STL库的map来提高搜索速度。
class LRUCache
{
public:
LRUCache(int capacity)
{
capacity_ = capacity;
length_ = 0;
list_ = NULL;
}
~LRUCache(){ Destroy();}
int get(int key);
void set(int key, int value);
void Print();
private:
struct ListNode
{
int key;
int value;
struct ListNode *next;
struct ListNode *pre;
ListNode(int k=-1,int val=-1, struct ListNode *p=NULL)
: key(k), value(val), next(p), pre(p){}
};
typedef struct ListNode ListNode; void Destroy();
void Delete(ListNode *node);
void DeleteRear();
ListNode* Find(int key);
ListNode* InsertFront(int key, int value); int length_;
int capacity_;
ListNode *list_;
map<int, ListNode*> map_;
}; //value is positive
//if not found, return -1
int LRUCache::get(int key)
{
int x;
ListNode *node(NULL);
if(map_.count(key))
{
node = map_.find(key)->second;
Delete(node);//do not free space
node->next = list_;
list_ = node;
return node->value;
}
else return -1;
} //if full, delete the least used, and insert into the head
void LRUCache::set(int key, int value)
{
ListNode *node(NULL);
if(map_.count(key))
{ //already in the list
node = map_.find(key)->second;
Delete(node);
node->value = value;
node->next = list_;
list_ = node;
}
else
{ //not found
node = InsertFront(key, value);
map_[key] = node;
if(length_ >= capacity_)
DeleteRear();
else
length_++;
}
} //return pointer to the node containing key
//if not found, return NULL
typename LRUCache::ListNode* LRUCache::Find(int key)
{
ListNode *list(list_); while(list && list->key != key)
list = list->next;
return (list!=NULL) ? list:NULL;
} //destroy singlely-linked list
void LRUCache::Destroy()
{
ListNode *pre(NULL); while(list_)
{
pre = list_;
list_ = list_->next;
delete pre;
}
map_.clear();
} void LRUCache::Delete(ListNode *node)
{ // do not free space
assert(node != NULL);
ListNode *pre(NULL), *cur(list_); while(cur != node)
{
pre = cur;
cur = cur->next;
}
assert(cur == node);
if(pre == NULL)
list_ = node->next;
else
pre->next = cur->next;
} void LRUCache::DeleteRear()
{
if(list_ == NULL) return;
ListNode *cur(list_), *pre(NULL); while(cur->next)
{
pre = cur;
cur = cur->next;
}
if(pre == NULL)
list_ = cur->next;
else
pre->next = cur->next;
map<int, ListNode*>::iterator it = map_.find(cur->key);
map_.erase(it);
delete cur;
} LRUCache::ListNode* LRUCache::InsertFront(int key, int val)
{
ListNode *node = new ListNode(key, val, list_);
list_ = node;
return node;
}
【Leetcode】 LRU Cache实现的更多相关文章
- [LeetCode] LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LeetCode]LRU Cache有个问题,求大神解答【已解决】
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- LeetCode:LRU Cache
题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...
- LeetCode——LRU Cache
Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...
- LeetCode: LRU Cache [146]
[题目] Design and implement a data structure for Least Recently Used (LRU) cache. It should support th ...
- LeetCode – LRU Cache (Java)
Problem Design and implement a data structure for Least Recently Used (LRU) cache. It should support ...
- Leetcode: LRU Cache 解题报告
LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should supp ...
- [LeetCode] LRU Cache [Forward]
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- Leetcode:LRU Cache,LFU Cache
在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...
- leetcode LRU Cache python
class Node(object): def __init__(self,k,x): self.key=k self.val=x self.prev=None self.next=None clas ...
随机推荐
- 纯css3代码写下拉菜单效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [saiku] 配置spring-security 允许 iframe加载saiku首页
最近提出了一个需求:在一个iframe中展现saiku首页 呵呵,这还不简单. 直接<iframe src="http://localhost:8080/saiku" /&g ...
- sql server多表数据批量更新
update wset w.TagCount=x.TagCountfrom (select ItemID,COUNT(*) as TagCount from r where IsValid=1 gro ...
- WordPress怎么在页面上添加目录
要实现的如下功能,在页面上添加一个文章目录: 步骤: 1)在wordpress中,在Posts----Categories中建立目录, 2) 3)add new post,指定post所属的cat ...
- JDE开发端安装笔记
JDE版本:JD Edwards EnterpriseOne 9.1.4 for Microsoft 64bit Oracle客户端:Oracle Database 11g Release 2 Cli ...
- ANGULARJS 动态编译添加到dom中
在使用angularjs 时,希望通过动态构建angular模版,再通过angular进行展示. 使用 方法如下: <html ng-app="app"> <he ...
- double int char 数据类型
贴心的limits... 测试代码: #include <iostream> #include <stdio.h> #include <limits> #inclu ...
- 查询使用NoLock
当我们在操作数据库的时候,无论是查询还是修改数据库的操作我们都习惯使用using(var db=new XXXDB()){},但是如果仅仅是做查询,最好是使用NoLock,因为NoLock使用的是共享 ...
- 保护WIFI无线网络的安全
本篇博客属于我们隐私与安全小贴士系列博客的一部分,其目的是确保您以及您的家人的上网安全.隐私与安全问题无论对我们还是对您都至关重要.我们在“不可 不知的小知识”网站上为您提供了如何安全,便捷地使用互联 ...
- windows-docker开发我常用命令 docker-machine ssh default
docker-machine ssh default docker logs test sudo systemctl start docker docker tag IMAGEID ne ...