#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 ...
随机推荐
- canvas绘制简单小铅笔
对应HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...
- MyBatis+Spring 事务管理
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://kinglixing.blog.51cto.com/34 ...
- 【学习】ABAP OLE 对EXCEL的处理
原文:http://blog.sina.com.cn/s/blog_7229b9c00100opx2.html -------------------------------------------- ...
- PowerMock注解PowerMockIgnore的使用方法
故事要从一个异常开始,某天我在开发一个加密.解密特性,算法使用的是3DES,样例代码如下. package org.jackie.study.powermock; import java.io.Uns ...
- 【JavaScript设计模式系列---开篇预览】
转:http://www.cnblogs.com/Darren_code/archive/2011/08/31/JavascripDesignPatterns.html 2011-08-31 23:5 ...
- A Cross-Platform Memory Leak Detector
Memory leakage has been a permanent annoyance for C/C++ programmers. Under MSVC, one useful feature ...
- Java基础知识强化之IO流笔记22:FileInputStream / FileOutputStream 复制文本文件案例1
1. 使用字节流FileInputStream / FileOutputStream 复制文本文件案例: 分析: (1)数据源:从哪里来 a.txt -- 读取数据 -- FileInpu ...
- ajax 基础教程
这是一本什么书?这是一本技术类的书籍,主要从历史.XMLHttpRequest对象.怎么样于服务器交互.构建完备的Ajax开发工具箱.使用jsUnit测试javascript 代码,总之就是让我们从这 ...
- 自己做的demo---关于java控制台输入跟类型转化跟处理异常的demo
package exception; import java.util.InputMismatchException; import java.util.Scanner; /*public class ...
- 使用linq语句获取指定条数的记录
//获得指定个数的子文件夹,用于分页 var pageAlbums = (from SPFolder pf in lstSubAlbums select pf) ...