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实现的更多相关文章

  1. [LeetCode] LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  2. [LeetCode]LRU Cache有个问题,求大神解答【已解决】

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  3. LeetCode:LRU Cache

    题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...

  4. LeetCode——LRU Cache

    Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...

  5. LeetCode: LRU Cache [146]

    [题目] Design and implement a data structure for Least Recently Used (LRU) cache. It should support th ...

  6. LeetCode – LRU Cache (Java)

    Problem Design and implement a data structure for Least Recently Used (LRU) cache. It should support ...

  7. Leetcode: LRU Cache 解题报告

    LRU Cache  Design and implement a data structure for Least Recently Used (LRU) cache. It should supp ...

  8. [LeetCode] LRU Cache [Forward]

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  9. Leetcode:LRU Cache,LFU Cache

    在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...

  10. 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 ...

随机推荐

  1. edm注意细节

    Email Direct Marketing不要有js,css也放在html里面不能有热区,所有的border要设置为0在浏览器里面邮件发送可能会歪掉,可以测试接受后是否歪掉 有点时候表格会有细缝等等 ...

  2. Farseer.Net

    Farseer.Net V0.2 ORM开源框架 目录 http://www.cnblogs.com/steden/archive/2013/01/22/2871160.html V1.0教程:htt ...

  3. JSON生成c#类代码小工具(转)

    原文地址: http://www.cnblogs.com/tianqiq/archive/2015/03/02/4309791.html

  4. soapUI参数

    点击File->New Rest Project,填入要测试的URI,确定进入编辑界面: 调整请求方式,添加请求参数,设置参数风格,这里要说一下:style有五种,QUERY是默认常用:TEMP ...

  5. 在列表页,按照指定的category取所属的post列表

    在某些指定的页面,例如news,blog等页面,需要列出指定某种类型的文章列表,这种情况下,可以先添加两个category,分别命名news,blog,然后再分别添加几个post,指定每个post所属 ...

  6. class、interface、struct的区别

    1 struct和class有什么区别 1.1默认的继承访问权限 Struct是public的,class是private的. 你可以写如下的代码: struct A { char a; }; str ...

  7. CSS3发光字动画

    ;} img{ width:200px; height:200px; border:2px solid #000} .back h5 { font-size: 4em; color: #f2050b; ...

  8. Java多线程-新特性-有返回值的线程

    在Java5之前,线程是没有返回值的,常常为了“有”返回值,破费周折,而且代码很不好写.或者干脆绕过这道坎,走别的路了. 现在Java终于有可返回值的任务(也可以叫做线程)了. 可返回值的任务必须实现 ...

  9. WPF的Binding功能解析

    1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...

  10. Servlet页面跳转实现方法的区别

    一直对Servlet页面跳转的几种方式理解的糊里糊涂的,今天在网上搜了一把,找到一遍比较好的,记下来,以后看看. Servlet页面跳转分两部分,一是发生在Servlet,一是在JSP,其实JSP也就 ...