#Leet Code# LRU Cache
语言: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的更多相关文章
- LeetCode之LRU Cache 最近最少使用算法 缓存设计
设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...
- LRU Cache 题解
题意 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- LeetCode题解: LRU Cache 缓存设计
LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode 版权声明:本文为博主原创文章,遵循CC 4 ...
- Go LRU Cache 抛砖引玉
目录 1. LRU Cache 2. container/list.go 2.1 list 数据结构 2.2 list 使用例子 3. transport.go connLRU 4. 结尾 正文 1. ...
- [LeetCode] LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 【leetcode】LRU Cache
题目简述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support t ...
- LeetCode:LRU Cache
题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...
- LRU Cache实现
最近在看Leveldb源码,里面用到LRU(Least Recently Used)缓存,所以自己动手来实现一下.LRU Cache通常实现方式为Hash Map + Double Linked Li ...
- 【leetcode】LRU Cache(hard)★
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
随机推荐
- hdu1698(线段树的区间替换)
HDU1698 #include <bits/stdc++.h> using namespace std; #define Maxn 1001000*4 struct Node{ int ...
- PHP如何取出数组最后一个元素?
<?php $array=array("first","sencond","third"); #1.echo end($array); ...
- PAT 1016. Phone Bills
A long-distance telephone company charges its customers by the following rules: Making a long-distan ...
- 【设计模式 - 24】之访问者模式(Visitor)
1 模式简介 访问者模式的定义: 访问者模式将数据结构与数据操作进行了分离,解决了稳定的数据结构和易变的数据操作的耦合问题. 访问者模式的优点: 1) 符合单一职责原则: 2) ...
- Sublime_text3怎么运行php代码
开发神奇sublime写代码真的好爽,之前听说是神器,但是没去用,觉得用eclipse写php代码,用dw写html够用了,用了一下sublime,哈哈,爽爆了. 除了写前端代码外,还需要写php代码 ...
- DateTime.TryParseExact 万能时间格式转化
本文转载:http://blog.csdn.net/gaofang2009/article/details/6073231 前天同事问C#有没有相关的方法能把"年月日时分秒"这样的 ...
- 【转】国内较快的maven镜像
国内连接maven官方的仓库更新依赖库,网速一般很慢,收集一些国内快速的maven仓库镜像以备用. ====================国内OSChina提供的镜像,非常不错=========== ...
- 【转】Android 混淆代码总结
http://blog.csdn.net/lovexjyong/article/details/24652085 为了防止自己的劳动成果被别人窃取,混淆代码能有效防止被反编译,下面来总结以下混淆代码的 ...
- Solr配置与简单Demo[转]
Solr配置与简单Demo 简介: solr是基于Lucene Java搜索库的企业级全文搜索引擎,目前是apache的一个项目.它的官方网址在http://lucene.apache.org/sol ...
- PHPExcel的读取excel的操作
首先导入类库: require_once 'PHPExcel.php'; require_once 'PHPExcel\IOFactory.php'; require_once 'PHPExcel\R ...