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

(nice!!!)(LeetCode 面试经典 150 题) 146. LRU 缓存 (哈希表+双向链表)

题目:146. LRU 缓存

在这里插入图片描述
在这里插入图片描述
思路:哈希表+双向链表,时间复杂度0(n)。

get、put时间复杂度必须为0(n),那么就得用空间换时间。双向链表来记录每一个节点node(key-value),哈希表来记录每一个key所对应的节点node。
在get、put时,先判断哈希表mp里是否存储了key,而双向链表是维护每一个节点的访问顺序。

C++版本:

class LRUCache {
// 节点node
typedef struct Node{int key;int val;Node * prev;Node * next;Node(int k,int v):key(k),val(v){}
}node;// 哈希表的最大容量
int capt;
// 双向链表的哨兵节点
node *head;
// 哈希表
unordered_map<int,node *> mp;public:// 将双向链表的节点x删除void deletee(node * x){x->prev->next=x->next;x->next->prev=x->prev;}// 将节点x插入到双向链表的头节点,也就是哨兵节点head的右边void insertt(node * x){x->prev=head;x->next=head->next;x->prev->next=x;x->next->prev=x;}// 初始化内置函数LRUCache(int capacity) {capt=capacity;head=new node(0,0);head->next=head;head->prev=head;}int get(int key) {if(mp.find(key)==mp.end()) return -1;deletee(mp[key]);insertt(mp[key]);return mp[key]->val;}void put(int key, int value) {// 哈希表存在keyif(mp.find(key)!=mp.end()){node * tmp=mp[key];tmp->val=value;deletee(tmp);insertt(tmp);return ;}// 哈希表不存在keymp[key]=new node(key,value);insertt(mp[key]);// 哈希表容量大于capacityif(mp.size()>capt){// 通过哨兵节点head找到最后一个节点node * bak=head->prev;deletee(bak);mp.erase(bak->key);delete bak;}}
};/*** 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);*/

JAVA版本:

class LRUCache {class node{int key;int val;node prev,next;node(int k,int v){key=k;val=v;}}int capt;node head=new node(0,0);Map<Integer,node> mp=new HashMap<>();void deletee(node  x){x.prev.next=x.next;x.next.prev=x.prev;}void insertt(node x){x.prev=head;x.next=head.next;x.prev.next=x;x.next.prev=x;}LRUCache(int capacity) {capt=capacity;head.next=head;head.prev=head;}int get(int key) {if(mp.containsKey(key)==false) return -1;deletee(mp.get(key));insertt(mp.get(key));return mp.get(key).val;}void put(int key, int value) {if(mp.containsKey(key)==true){node tmp=mp.get(key);tmp.val=value;deletee(tmp);insertt(tmp);return ;}mp.put(key,new node(key,value));insertt(mp.get(key));if(mp.size()>capt){node bak=head.prev;deletee(bak);mp.remove(bak.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);*/

GO版本:

type node struct{key,val    intprev,next  *node
}type LRUCache struct {capacity inthead     *nodemp       map[int]*node
}func Constructor(capacity int) LRUCache {nd:=&node{key:0,val:0,}nd.prev=ndnd.next=ndreturn LRUCache{capacity :capacity,head     :nd,mp       :map[int]*node{},}
}func (this *LRUCache) deletee(x *node){x.prev.next=x.nextx.next.prev=x.prev
}func (this *LRUCache) insertt(x *node){x.prev=this.headx.next=this.head.nextx.prev.next=xx.next.prev=x
}func (this *LRUCache) getNode(key int) *node{nd,ok :=this.mp[key]if !ok {return nil}this.deletee(nd)this.insertt(nd)return nd
}func (this *LRUCache) Get(key int) int {nd:=this.getNode(key)if nd==nil {return -1}return nd.val
}func (this *LRUCache) Put(key int, value int)  {nd:=this.getNode(key)if nd!=nil {nd.val=valuereturn}this.mp[key]=&node{key :key,val :value,}this.insertt(this.mp[key])if len(this.mp)>this.capacity {bak:=this.head.prevthis.deletee(bak)delete(this.mp,bak.key)}return 
}/*** Your LRUCache object will be instantiated and called as such:* obj := Constructor(capacity);* param_1 := obj.Get(key);* obj.Put(key,value);*/
http://www.xdnf.cn/news/1270315.html

相关文章:

  • 力扣热题100------70.爬楼梯
  • 如何解决 Vue 项目启动时出现的 “No such module: http_parser” 错误问题
  • Cherryusb UAC例程对接STM32内置ADC和DAC播放音乐和录音(中)=>UAC+STM32 ADC+DAC实现录音和播放
  • traceroute命令调试网络
  • C++高频知识点(十七)
  • 《Resolving tissue complexity by multimodal spatial omics modeling with MISO》
  • 9. 堆和栈有什么区别
  • Vitalik谈以太坊:ETH财库储备策略“有益且有价值”
  • Kotlin 协程线程切换机制详解
  • AG32cpld实现一个UartTx“外设”
  • 智慧能源设备巡检缺陷漏检率↓76%:陌讯多模态融合算法实战解析
  • Android适配最新SplashScreen方案:让启动页不再“翻车“
  • webrtc弱网-BandwidthQualityScaler 源码分析与算法原理
  • 视图是什么?有什么用?什么时候用?MySQL中的视图
  • Android MediaCodec 音视频编解码技术详解
  • linux php版本降级,dnf版本控制
  • Amazon Linux 训练lora模型的方式
  • Web自动化技术选择
  • 回答“http协议 ,js组件化,工程化, seo优化策略 ,针对不同平台终端适配 web标注和兼容性”
  • 基于遗传优化的智能灌溉系统控制策略matlab仿真
  • Beelzebub靶机通关教程
  • 【工具】Python多环境管理
  • 【Java基础】字符串不可变性、string的intern原理
  • 【李宏毅-2024】第六讲 大语言模型的训练过程1——预训练(Pre-training)
  • 搭建若依前后端分离版本的开发环境
  • 鸿蒙分布式任务调度深度剖析:跨设备并行计算的最佳实践
  • 在nodejs中使用Java方法
  • windows、linux应急响应入侵排查
  • React中实现完整的登录鉴权与权限控制系统
  • 云服务器--阿里云OSS(2)【Springboot使用阿里云OSS】