#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 ...
随机推荐
- 一个好看的Input样式
<div class="search"> <input type="text"></div> .search{ text-a ...
- SourceTree的基本使用
1. SourceTree是什么 拥有可视化界面的项目版本控制软件,适用于git项目管理 window.mac可用 2. 获取项目代码 1. 点击克隆/新建 2. 在弹出框中输入项目地址,http或者 ...
- web 网站安全证书已过期或不可信 是否继续浏览
发生环境:魅族MX4 uc浏览器 IIS部署SSL证书后提示不可信的解决方案 第一步:打开mmc——点击文件——添加删除管理单元——证书——计算机帐户 第二步:在计算机帐户的个人证书里面导入pfx格 ...
- win7家庭版升级旗舰版
点“开始”——在“所有程序”点"Windows Anytime Update"——点“输入升级密钥”,然后就出现一个密钥框,输入一个旗舰版的密钥,确定就行了,10分钟左右就升级好了 ...
- hdu2018java
母牛的故事 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- ThreadLocal 笔记
synchronized 同步的机制可以解决多线程并发问题,这种解决方案下,多个线程访问到的都是同一份变量的内容.为了防止在多线程访问的过程中,可能会出现的并发错误.不得不对多个线程的访问进行同步,这 ...
- iOS UIKit:TableView之表格创建(1)
Table View是UITableView类的实例对象,其是使用节(section)来描述信息的一种滚动列表.但与普通的表格不同,tableView只有一行,且只能在垂直方向进行滚动.tableVi ...
- Linux服务器常用性能监控命令汇总
1.ifconfig 网卡数目.ip地址.Mac地址.MTU大小 eth0 Link encap:Ethernet HWaddr 00:0d:3a:50:12:e9 inet addr:10.0.0. ...
- Java-Android 之Hello World
1.新建一个Android Project 2.2版本的 修改values下面的内容,为: <?xml version="1.0" encoding="utf-8& ...
- [视频转换] C#VideoConvert视频转换帮助类 (转载)
点击下载 VideoConvert.zip 主要功能如下 .获取文件的名字 .获取文件扩展名 .获取文件类型 .视频格式转为Flv .生成Flv视频的缩略图 .转换文件并保存在指定文件夹下 .转换文件 ...