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. python 第三方模块 转 https://github.com/masterpy/zwpy_lst

    Chardet,字符编码探测器,可以自动检测文本.网页.xml的编码. colorama,主要用来给文本添加各种颜色,并且非常简单易用. Prettytable,主要用于在终端或浏览器端构建格式化的输 ...

  2. C/C++ 如何劫持别人家的命令||函数||程序(只能对于window而言)

    要实现下面程序,首先我们需要三个文件 detours.h ,detours.lib ,detver.h(可以去网上下载) 1. 首先让我们看看,一个最简单的C程序,如何劫持system函数. #inc ...

  3. mysql关于编码部分(乱码出现的原因和解决方法)

    在使用mysql客户端时,我们会经常出现一个这样一个问题,就是原先好好文字,怎么输入之后就出现乱码了呢? 出现这样的问题: 第一个原因: 可能是这是我们新安装的一个mysql,没有经过配置,第二个原因 ...

  4.  BP神经网络

     BP神经网络基本原理 BP神经网络是一种单向传播的多层前向网络,具有三层或多层以上的神经网络结构,其中包含输入层.隐含层和输出层的三层网络应用最为普遍. 网络中的上下层之间实现全连接,而每层神经元之 ...

  5. 实验三 敏捷开发与XP实践(改)

    ---恢复内容开始--- 一.敏捷开发与XP 二.编码标准 1.编码标准中的版式就是一个很好的例子,版式虽然不会影响程序的功能,但会影响可读性.程序的版式追求清晰.美观,是程序风格的重要因素.单击Ec ...

  6. MySQL 命令杂记

    mysql> show processlist; 如果是root帐号,你能看到所有用户的当前连接.如果是其它普通帐号,只能看到自己占用的连接.show processlist;只列出前100条, ...

  7. noip知识点总结之--线性筛法及其拓展

    一.线性筛法 众所周知...线性筛就是在O(n)的时间里找出所有素数的方法 code: void get_prime(int N){ int i, j, k; memset(Flag, ); ; i ...

  8. 源码编译Chrome

    官网描述 http://www.chromium.org/developers/how-tos/build-instructions-windows 为啥还要写这篇博客 太久没在这里写博客 Chrom ...

  9. mke2fs/mks.etc3/fstab/mount指令

    一.mke2fs指令mkfs.etc3 /dev/sdb1指令 主要新学习 cat /etc/filesystem  //查看文件类型 mkfs. tab键有提示    //按照系统默认的值格式化 m ...

  10. java面向对象编程—— 第三章 程序流程控制

    3.1流程控制 三种基本技术可以改变程序的控制流程: ①   调用方法:调用方法将导致控制流程离开当前方法,转移到被调用的方法: ②   选择:java中有两种做出选择的机制:if/else语句和sw ...