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. 全面理解面向对象的 JavaScript

    前言 当今 JavaScript 大行其道,各种应用对其依赖日深.web 程序员已逐渐习惯使用各种优秀的 JavaScript 框架快速开发 Web 应用,从而忽略了对原生 JavaScript 的学 ...

  2. 《Java程序设计》第十周学习总结

    20145224 <Java程序设计>第十周学习总结 网络编程 ·网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的 ...

  3. Compound Interest Calculator3.0

    Compound Interest Calculator3.0 1.利率这么低,复利计算收益都这么厉害了,如果拿100万元去买年报酬率10%的股票,若一切顺利,过多长时间,100万元就变成200万元呢 ...

  4. WindowsService(Windows服务)开发步骤附Demo 【转】

    转http://www.cnblogs.com/moretry/p/4149489.html 1.打开VS,新建项目,选择Windows服务,然后设置目录及项目名称后点击确定. 2.展开Service ...

  5. OSVERSIONINFO

    OSVERSIONINFO结构 OSVERSIONINFO结构包含了操作系统的版本信息,包括操作系统的主版本号.副版本号.创建号.以及操作系统平台ID号和关于操作系统的其他描述信息.其定义为: typ ...

  6. android:configChanges属性

    对android:configChanges属性,一般认为有以下几点: 1.不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏 ...

  7. 意外的节点类型 Element。只能在简单内容或空内容上调用 ReadElementString 方法

    问题出现的情景: 在调用携程团购接口时,需要把获取的xml字符串反序列化实体对象,出现了这个错误. 详情: 在对xml文档有这样一条语句“ <Description Category=" ...

  8. eclipse 项目修改和更新项目,回退版本,解决分支的冲突的办法

    一个关于git的图 1.我在github建立了3个分支. 2.把其中一个分支拉到本地. 项目修改提交到远程库 3.修改完代码以后commit项目,点击项目右击->team->commit ...

  9. 图形界面报错“已拒绝X11转移申请”的解决方法

    今天想通过本机给虚拟机起x-manager图形界面的时候报出 解决办法: 1.原来X11 forwarding依赖“xorg-x11-xauth”软件包,所以必须先安装“xorg-x11-xauth” ...

  10. Octopus系列之开发中灵光点收集,先放到这里,后面会整理的

    项目中引用的组件 1.System.Data.SQLite.dll 自行编译 SQLite-1.0.66.0-source 3.5的框架:F:\Code\开源项目\SQLite\1.0.66.0_x8 ...