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. 25套用于Web UI设计的免费PSD网页元素模板

    Web 元素是任何网站相关项目都需要的,质量和良好设计的元素对于设计师来说就像宝贝一样.如果您正在为您的网站,博客,Web 应用程序或移动应用程序寻找完美设计的网页元素,那么下面这个列表会是你需要的. ...

  2. 小程序---根据数据库反向生成java文件

    工作中写entry太繁琐,写了一个小程序反向生成.从而大大减少了工作量 import java.io.File; import java.io.FileWriter; import java.io.I ...

  3. UB单修改

    FUNCTION Z_SD_UB_CHANGE. *"-------------------------------------------------------------------- ...

  4. mongodb 和 mysql 的对照

    In addition to the charts that follow, you might want to consider the Frequently Asked Questions sec ...

  5. FZU 2093 找兔子 状压DP

    题目链接:找兔子 n的范围是[1, 15],可以用0 到 (1<<n)-1 的数表示全部状态,用dp[i] = t表示到达状态i的最少时间是t,对于每个点,如果它能到达的所有点在t秒时都已 ...

  6. eclipse关联tomcat并且部署java web应用程序

    http://www.ibm.com/developerworks/cn/opensource/os-eclipse-tomcat/

  7. js基础之DOM

    一.创建子节点 发帖在顶部显示: var oBtn = document.getElementById('btn1'); var oUl = document.getElementById('ul1' ...

  8. DataGridView绑定数据库,取得的数据插入到DataGridView指定列(一)

    实现: 点击button1,从数据库中获得数据,指定数据库的某列数据插入到DataGridView指定列 一.双击button1进入事件代码 private void button1_Click(ob ...

  9. centos=>gsutil,iptables

    sudo apt-get remove --purge gsutil sudo easy_install -U pip  sudo pip2 install gsutil gsutil ls gs:/ ...

  10. (DFS)hdoj1312-Red and Black

    题目链接 非常简单的DFS,初学DFS做这道题很合适.需要注意的是题目中输入的行和列顺序是颠倒的. #include<cstdio> #include<cstring> usi ...