当前位置: 首页 > news >正文

146.LRU缓存

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
双向链表+哈希表

class LRUCache {
public://1、定义双向链表结构、容量、哈希表等LRU数据成员struct Node{int key,value;Node *left,*right;Node(int _key,int _value):key(_key),value(_value),left(NULL),right(NULL){}}*L,*R;int n;unordered_map<int,Node*> ump;//2、初始化LRU缓冲,容量为capacityLRUCache(int capacity) {n=capacity;//L、R的分配内存及初始化L=new Node(-1,-1);R=new Node(-1,-1);L->right=R;R->left=L; }//3、定义insert、remove操作void remove(Node *p){p->left->right=p->right;p->right->left=p->left;}//链表左侧为活跃节点,insert位置void insert(Node* p){L->right->left=p;p->right=L->right;p->left=L;L->right=p;}int get(int key) {if(ump.count(key)==0) return -1;Node *p=ump[key];remove(p);insert(p);return p->value;}void put(int key, int value) {if(ump.find(key)!=ump.end()){Node* p=ump[key];remove(p);insert(p);p->value=value;}else{if(ump.size()==n){Node *tmp=R->left;ump.erase(tmp->key);remove(tmp);delete tmp;}Node *p=new Node(key,value);insert(p);ump[key]=p;}}
};
http://www.lryc.cn/news/197805.html

相关文章:

  • 使用transformers过程中出现的bug
  • Hadoop3教程(二十二):Yarn的基础架构与工作流程
  • 离线 notepad++ 添加到右键菜单
  • 怎么让英文大语言模型支持中文?--构建中文tokenization--继续预训练--指令微调
  • 笙默考试管理系统-MyExamTest----codemirror(35)
  • MMKV(2)
  • Spring Boot项目中使用 TrueLicense 生成和验证License(附源码)
  • ES6 Iterator 和 for...of 循环
  • ubuntu20.04 nvidia显卡驱动掉了,变成开源驱动,在软件与更新里选择专有驱动,下载出错,调整ubuntu镜像源之后成功修复
  • 华为FAT模式无线AP配置实例
  • nodejs基于vue 学生论坛设计与实现
  • 017 基于Spring Boot的食堂管理系统
  • 常用的二十种设计模式(下)-C++
  • C#桶排序算法
  • 快速了解服务器单CPU与双CPU
  • c# Dictionary、ConcurrentDictionary的使用
  • 大数据中间件——Kafka
  • HarmonyOS/OpenHarmony原生应用-ArkTS万能卡片组件Slider
  • SpringCloud: sentinel链路限流
  • UML 中的关系
  • ChatGPT技术或加剧钓鱼邮件攻击
  • 哨兵1号后向散射系数土壤水分反演
  • day3:Node.js 基础知识
  • 【RDMA】librdmacm库和连接建立过程
  • 如何使用Python抓取PDF文件并自动下载到本地
  • 人脸写真FaceChain的简单部署记录(一)
  • linux虚机新增加磁盘后在系统中查不到
  • js中隐式类型转换与toPrimitive
  • 家政系统预约小程序具备哪些功能?
  • 【LeetCode】46. 全排列