设计数据结构之LRU缓存
class LRUCache {
private:
struct Node {
int key;
int val;
Node* prev;
Node* next;
Node(int k, int v):key(k), val(v), prev(NULL), next(NULL){}
Node() = default;
};
void removeLastNode(){
auto last = tail->prev;
last->prev->next = tail;
tail->prev = last->prev;
cache.erase(last->key);
delete last;
}
void insertHead(Node* node){
node->next = head->next;
head->next->prev = node;
head->next = node;
node->prev = head;
}
void detachNode(Node* node){
node->prev->next = node->next;
node->next->prev = node->prev;
}
Node* head;
Node* tail;
int cap;
unordered_map<int, Node*> cache;
public:
LRUCache(int capacity) {
head = new Node;
tail = new Node;
head->next = tail;
tail->prev = head;
cap = capacity;
}
int get(int key) {
if(cache.find(key) == cache.end()) return -1;
detachNode(cache[key]);
insertHead(cache[key]);
return cache[key]->val;
}
void put(int key, int value) {
if(cache.find(key) != cache.end()){
cache[key]->val = value;
detachNode(cache[key]);
} else {
Node* newNode = new Node(key, value);
if(cache.size() == cap) removeLastNode();
cache[key] = newNode;
}
insertHead(cache[key]);
}
};
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/
设计数据结构之LRU缓存的更多相关文章
- 面试挂在了 LRU 缓存算法设计上
好吧,有人可能觉得我标题党了,但我想告诉你们的是,前阵子面试确实挂在了 RLU 缓存算法的设计上了.当时做题的时候,自己想的太多了,感觉设计一个 LRU(Least recently used) 缓存 ...
- LeetCode:146_LRU cache | LRU缓存设计 | Hard
题目:LRU cache Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...
- 《数据结构与算法之美》 <04>链表(上):如何实现LRU缓存淘汰算法?
今天我们来聊聊“链表(Linked list)”这个数据结构.学习链表有什么用呢?为了回答这个问题,我们先来讨论一个经典的链表应用场景,那就是 LRU 缓存淘汰算法. 缓存是一种提高数据读取性能的技术 ...
- 数据结构与算法之美 06 | 链表(上)-如何实现LRU缓存淘汰算法
常见的缓存淘汰策略: 先进先出 FIFO 最少使用LFU(Least Frequently Used) 最近最少使用 LRU(Least Recently Used) 链表定义: 链表也是线性表的一种 ...
- 请用Java设计一个Least Recently Used (LRU) 缓存
LRU介绍:LRU是Least Recently Used的缩写,即最少使用页面置换算法,是为虚拟页式存储管理服务的, 思路介绍: 能够使用两个标准的数据结构来实现.Map和Queue.由于须要支持多 ...
- LRU缓存实现(Java)
LRU Cache的LinkedHashMap实现 LRU Cache的链表+HashMap实现 LinkedHashMap的FIFO实现 调用示例 LRU是Least Recently Used 的 ...
- 04 | 链表(上):如何实现LRU缓存淘汰算法?
今天我们来聊聊“链表(Linked list)”这个数据结构.学习链表有什么用呢?为了回答这个问题,我们先来讨论一个经典的链表应用场景,那就是+LRU+缓存淘汰算法. 缓存是一种提高数据读取性能的技术 ...
- 146. LRU缓存机制
题目描述 运用你所掌握的数据结构,设计和实现一个LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果密钥 (key ...
- W-TinyLFU——设计一个现代的缓存
缓存设计是个基础架构领域里的重要话题,本号之前也有谈论过相关话题,点击原文可以看之前的介绍. 近日,HighScalability网站刊登了一篇文章,由前Google工程师发明的W-TinyLFU—— ...
随机推荐
- UITableView的使用总结
直接贴代码了,很好理解,注释很全,一看就懂...... // // ViewController.m // TableViewSectionTitleDemo // // Created by 思 彭 ...
- 【css】纯css实现文字循环滚动效果
不用js来实现. html: <div class="box"> <p class="animate"> 文字滚动的内容文字滚动的内容文 ...
- nvm 安装及操作 node版本管理
安装 > curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash 安装完成后重启下 ...
- 论文翻译:LP-3DCNN: Unveiling Local Phase in 3D Convolutional Neural Networks
引言 传统的3D卷积神经网络(CNN)计算成本高,内存密集,容易过度拟合,最重要的是,需要改进其特征学习能力.为了解决这些问题,我们提出了整流局部相位体积(ReLPV)模块,它是标准3D卷积层的有效替 ...
- TensorFlow实战第四课(tensorboard数据可视化)
tensorboard可视化工具 tensorboard是tensorflow的可视化工具,通过这个工具我们可以很清楚的看到整个神经网络的结构及框架. 通过之前展示的代码,我们进行修改从而展示其神经网 ...
- 【Linux开发】linux设备驱动归纳总结(六):1.中断的实现
linux设备驱动归纳总结(六):1.中断的实现 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- 【转帖】msvcp100.dll和msvcr100.dll
VS发布软件时去除msvcp100.dll和msvcr100.dll图解说明 https://blog.csdn.net/yu__jia/article/details/82753262 msvcp. ...
- java-selenium-java鼠标键盘操作Actions类和Robot
Actions类 一.鼠标右击.双击 Java代码 //定位百度首页右上角 新闻 WebElement Xw=driver.findElement(By.xpath("//*[@id='u1 ...
- JAVA基础--JAVA API常见对象(字符串&缓冲区)
一. String 类型 1. String类引入 第二天学习过Java中的常量: 常量的分类: 数值型常量:整数,小数(浮点数) 字符型常量:使用单引号引用的数据 字符串常量:使用双引号引用 ...
- FFmpeg4.0笔记:封装ffmpeg的视频帧转换功能类CSws
Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CSws.h /************************* ...