#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 ...
随机推荐
- C#实现SOAP调用WebService
最近写了一个SOA服务,开始觉得别人拿到我的服务地址,然后直接添加引用就可以使用了,结果"大牛"告知不行. 让我写一个SOAP调用服务的样例,我有点愣了,因为没做过这方面的,于是搞 ...
- PHP的curl实现get,post 和 cookie(实例)
类似于dreamhost这类主机服务商,是显示fopen的使用 的.使用php的curl可以实现支持FTP.FTPS.HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE ...
- jQuery.validate 中文API
名称 返回类型 描述 validate(options) 返回:Validator 验证所选的FORM valid() 返回:Boolean 检查是否验证通过 rules() 返回:Options ...
- 关于PHP开发的9条建议
这篇文章主要介绍了关于PHP开发的9条建议,都是个人的一些经验总结,有需要的小伙伴可以参考下. 本文只是个人从实际开发经验中总结的一些东西,并不是什么名言警句,写出来有两个目的:一是时刻提醒自己要按照 ...
- 用C#实现通用守护进程
1. 下载 源码下载:http://pan.baidu.com/s/1vqDA2 安装包下载:http://pan.baidu.com/s/1sjmEB0p 2. 安装注意事项 在配置档中配置你要守护 ...
- MYSQL 系统命令 源码定位
sql_cmd.h enum enum_sql_command { SQLCOM_SELECT, SQLCOM_CREATE_TABLE, SQLCOM_CREATE_INDEX, SQLCOM_AL ...
- linux-0.11内核 调试教程+GCC源代码
http://pan.baidu.com/share/home?uk=453348606&view=share#category/type=0 http://blog.csdn.net/old ...
- Android开发之使用活动显示对话框
利用活动显示对话框,需要重写Activity中的onCreateDialog()方法,以此来显示一个对话框窗口. 效果如下: 实现代码如下: package com.example.dialog; i ...
- CentOS 6.7 配置nginx支持SSL/https访问
一.安装必要的包 yum install openssl openssl-devel 二.配置编译参数,增加对SSL的支持 ./configure –with-http_ssl_module 三.修改 ...
- Linux下rsync增加SSH端口号的用法
rsync默认使用SSH的22号端口,为了安全起见,很多机器更改了SSH默认的端口号,对应rsync命令的用法为: rsync -e 'ssh -p 1234' username@hostname:S ...