语言:C++

描述:使用单链表实现,HeadNode是key=-1,value=-1,next=NULL的结点。距离HeadNode近的结点是使用频度最小的Node。

 struct Node {
int key;
int value;
Node* next;
}; class LRUCache {
public:
LRUCache(int capacity) {
this->capacity = capacity;
this->curLength = ; this->headNode = new Node();
this->headNode->key = -;
this->headNode->value = -;
this->headNode->next = NULL; this->lastNode = this->headNode;
} int get(int key) {
if (this->curLength == )
return -; Node* tmpNode = reSequencing(key, -);
if (tmpNode != NULL) {
return tmpNode->value;
} else {
return -;
}
} void set(int key, int value) {
if (this->capacity == ) return; Node* tmpNode = reSequencing(key, value);
if (tmpNode != NULL) {
tmpNode->value = value;
return;
} if (this->curLength + > this->capacity) {
if (this->headNode->next == this->lastNode) {
delete this->lastNode;
this->lastNode = this->headNode;
} else {
Node* t = this->headNode->next->next;
delete this->headNode->next;
this->headNode->next = t;
}
} Node* newNode = new Node();
newNode->key = key;
newNode->value = value;
newNode->next = NULL; this->lastNode->next = newNode;
this->lastNode = newNode; curLength += ;
} Node* reSequencing(int key, int value) {
Node* tmpNode = this->headNode;
Node* preNode; while (tmpNode != NULL) {
if (tmpNode->key == key) {
break;
} preNode = tmpNode;
tmpNode = tmpNode->next;
} if (tmpNode != NULL && this->lastNode->key != key) {
preNode->next = tmpNode->next; this->lastNode->next = tmpNode;
this->lastNode = tmpNode;
tmpNode->next = NULL;
} return tmpNode;
} private:
int capacity;
int curLength; Node* headNode;
Node* lastNode;
};

#Leet Code# LRU Cache的更多相关文章

  1. LeetCode之LRU Cache 最近最少使用算法 缓存设计

    设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...

  2. LRU Cache 题解

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

  3. LeetCode题解: LRU Cache 缓存设计

    LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode   版权声明:本文为博主原创文章,遵循CC 4 ...

  4. Go LRU Cache 抛砖引玉

    目录 1. LRU Cache 2. container/list.go 2.1 list 数据结构 2.2 list 使用例子 3. transport.go connLRU 4. 结尾 正文 1. ...

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

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

  6. 【leetcode】LRU Cache

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

  7. LeetCode:LRU Cache

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

  8. LRU Cache实现

    最近在看Leveldb源码,里面用到LRU(Least Recently Used)缓存,所以自己动手来实现一下.LRU Cache通常实现方式为Hash Map + Double Linked Li ...

  9. 【leetcode】LRU Cache(hard)★

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

随机推荐

  1. c#+ArcEngine中的IGroupLayer的用法

    转自羊子雄起原文c#+ArcEngine中的IGroupLayer的用法 在AE开发中,我们知道axMapControl.LayerCount能获取图层的数量,但是这种方法不能获取到图层组里面的图层, ...

  2. swift小结02-基础篇

    闭包      类似于 OC 中的 Block,是一段预先定义好的代码,在需要时执行   闭包表达式格式:  { (形参名称1: 形参类型1, 形参名称2: 形参类型2, ...) -> 返回值 ...

  3. Lamda和Linq语法对比详细

    本人转载:http://www.cnblogs.com/knowledgesea/p/3897665.html 闲言碎语 近期比较忙,但还是想写点什么,就分享一些基础的知识给大家看吧,希望能帮助一些l ...

  4. WebService基于SoapHeader实现安全认证(一)

    本文转载:http://www.cnblogs.com/houleixx/archive/2009/08/22/webservice-soapheader-security.html WebServi ...

  5. JSP内置对象整理(转)

    ① out - javax.servlet.jsp.jspWriter out对象用于把结果输出到网页上. 方法: 1. void clear() ;清除输出缓冲区的内容,但是不输出到客户端. 2. ...

  6. c++使用mysql的api连接相关问题

    记录一下自己使用中的相关问题,方便有相同问题的同学解决. 关于在VS中的各种配置.看这里.只是须要注意一下,我如今用的mysql版本号是5.6的,已经没有[MySQL Server \lib\opt] ...

  7. Nginx vs Apache--reference

    May 14th, 2014 - By Walker Rowe https://anturis.com/blog/nginx-vs-apache/ What is the Nginx web and ...

  8. favicon.ico显示,favicon显示,favicon图标显示

    favicon.ico显示,favicon显示,favicon图标显示 >>>>>>>>>>>>>>>> ...

  9. C语言基础知识小总结(2)

    上个总结比较笼统,下面写的稍微详细点吧算是.   一.控制台屏幕打印 1.putchar();   格式: void putchar(char c);  //直接把一个字符输出到屏幕上 2.print ...

  10. MYSQL 基础操作

    1.MySQL基础操作 一:MySQL基础操作 1:MySQL表复制 复制表结构 + 复制表数据 create table t3 like t1; --创建一个和t1一样的表,用like(表结构也一样 ...