语言: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. php 获取某个月的周次信息

    在做统计的时候如果按照周统计 ,需要对某个月的周次信息进行计算,如果本月头一天不是星期一,则向上一个月取周一,本月最后的几天如果不能正好是一周,则忽略. 例如 2019-09月计算出来的结果 2016 ...

  2. asp.net redis 实战

    当开始用    var result=new RedisClient("127.0.0.1",6379,1"1111");  这个helper ,后面有并发之后 ...

  3. tomcat install on Linux

    1)下载apache-tomcat-6.0.10.tar.gz 2)#tar -zxvf apache-tomcat-6.0.10.tar.gz ://解压 3)#cp -R apache-tomca ...

  4. OpenStack_Swift源代码分析——创建Ring及加入�设备源代码算法具体分析

    1 创建Ring 代码具体分析 在OpenStack_Swift--Ring组织架构中我们具体分析了Ring的具体工作过程,以下就Ring中添加�设备,删除设备,已经又一次平衡的实现过程作具体的介绍. ...

  5. view中的setTag和getTag方法的理解

    下面是一段自定义适配器中的getView方法,其中使用了view的一个setTag和getTag方法 View中的setTag(Onbect)表示给View添加一个格外的数据(相当于缓存),以后可以用 ...

  6. '[linux下tomcat 配置

    tomcat目录结构 bin ——Tomcat执行脚本目录 conf ——Tomcat配置文件 lib ——Tomcat运行需要的库文件(JARS) logs ——Tomcat执行时的LOG文件 te ...

  7. UTF-8 BOM对PHP的影响

    今天在用notepad++写代码时 载入一个frameset框架模版后 在页面上一直不显示该页面,查看源码后都正常.然后索性把里面东西全删掉 随便写了几个测试文字可以正常显示. 折腾了好长时间,最后偶 ...

  8. C#开发的进化史

    1.数据类型的进化 C#1中实现Product类型代码 public class Product { string name; public string Name { get { return na ...

  9. MySQL数字类型中的三种常用种类

    数字类型 MySQL数字类型按照我的分类方法分为三类:整数类.小数类和数字类. MySQL数字类型之一我所谓的“数字类” 就是指 DECIMAL 和 NUMERIC,它们是同一种类型.它严格的说不是一 ...

  10. Java SE (1)之 JFrame 组件 FlowLayout 布局

    package com.sunzhiyan; import java.awt.*; import java.awt.event.*; import javax.swing.*; public clas ...