力扣链接:146. LRU 缓存机制

思路:哈希表 + 双向链表

为什么必须要用双向链表?

因为我们需要删除操作。删除一个节点不光要得到该节点本身的指针,也需要操作其前驱节点的指针,而双向链表才能支持直接查找前驱,保证操作的时间复杂度 O(1)。

为什么要在链表中同时存储 key 和 val,而不是只存储 val?

当缓存容量已满,我们不仅仅要删除最后一个节点,还要把哈希表 中映射到该节点的 key 同时删除,而这个 key 只能由 节点得到。如果 节点结构中只存储 val,那么我们就无法得知 key 是什么,就无法删除 哈希表中的键,造成错误。

代码

我这里是向尾部添加数据,所以头部的是不活跃的数据值

type LRUCache struct {  //LRU 缓存结构
capacity int // 容量
m map[int]*Node //哈希表
cache *NodeList //双向链表
} type Node struct{ //节点结构
key int
value int
prev *Node //前一个节点
next *Node //后一个节点
} func initNode(key,value int)*Node{ //初始化节点
return &Node{
key:key,
value:value,
}
} type NodeList struct{ //链表结构
head *Node //链表头节点
last *Node //链表尾节点
size int //元素个数
} func initList ()*NodeList{ //初始化链表
dil:=&NodeList{
head:initNode(0,0),
last:initNode(0,0),
size:0,
}
dil.head.next=dil.last
dil.last.prev=dil.head return dil
} func (this *NodeList)addNodeinlist(node *Node){ //向链表中插入节点,向链表尾部插节点
node.prev=this.last.prev
this.last.prev=node
node.prev.next=node
node.next=this.last this.size++
} func (this *NodeList)deleteNodeinlist (node *Node){ //删除链表中的某一结点
node.prev.next=node.next
node.next.prev=node.prev
node.next=nil
node.prev=nil
this.size--
} func (this *NodeList)delfirstNode()*Node{ //删除第一个节点,并且返回
if this.head.next==this.last{
return nil
}
t:=this.head.next
this.deleteNodeinlist(t) return t
} func Constructor(capacity int) LRUCache { //初始化 LRU 缓存
return LRUCache{
capacity:capacity,
m:make(map[int]*Node,0),
cache:initList(),
}
} func (this *LRUCache)addkey(key,value int){ //添加元素
node:=initNode(key,value)
//增加map映射
this.m[key]=node //在链表中添加元素
this.cache.addNodeinlist(node)
} func (this *LRUCache)makekey(key int){ // 将某个 key 调整为最近使用的元素
//找到节点
node:=this.m[key]
//删除节点
this.cache.deleteNodeinlist(node)
// 添加到链表尾部
this.cache.addNodeinlist(node)
} func (this *LRUCache)deletekey(key int){ //删除元素 //删除链表中节点
this.cache.deleteNodeinlist(this.m[key])
//删除map映射
delete(this.m,key)
} func (this *LRUCache)deletefirkey(){ //删除最久未使用的元素
// 链表的第一个就是最近最少使用的元素
node:=this.cache.delfirstNode() // 删除映射
delete(this.m,node.key)
} func (this *LRUCache) Get(key int) int {
if _,ok:=this.m[key];ok{
//存在
this.makekey(key) //将某个 key 调整为最近使用的元素
return this.m[key].value
}else{
//不存在
return -1
} } func (this *LRUCache) Put(key int, value int) {
// 检查key存不存在
if _,ok:=this.m[key];ok{
//存在
//删除元素
this.deletekey(key) //添加元素到尾部
this.addkey(key,value) }else{
//不存在
if this.capacity==this.cache.size{
//缓存达到上限
//删除最久未使用的元素
this.deletefirkey()
}
//添加元素到尾部
this.addkey(key,value)
}
}

参考:

https://leetcode-cn.com/problems/lru-cache/solution/jian-dan-shi-li-xiang-xi-jiang-jie-lru-s-exsd/

最新文章

  1. 【BZOJ-2177】曼哈顿最小生成树 Kruskal + 树状数组
  2. MVC,布局页面
  3. Hibernate内存溢出分析一例
  4. UVa 10791 (唯一分解) Minimum Sum LCM
  5. 分布式文件系统-HDFS
  6. PLSQL存储过程校验身份证
  7. hdu 5562 Clarke and food(贪心)
  8. JobTracker等相关功能模块初始化
  9. EL表达式学习
  10. $.ajax和$.post的区别(前者根据key-value/后者根据形参)
  11. 使用COE脚本绑定SQL Profile
  12. Chapter3_操作符_关系操作符
  13. Salesforce随笔: 将Visualforce Page导出为 Excel/CSV/txt (Display a page in Excel)
  14. 译:2. RabbitMQ Java Client 之 Work Queues (工作队列)
  15. [kx]人眼结构&凹/凸透镜成像及生活应用
  16. IT技术公众号推荐
  17. 系统数据库--恢复Master数据库
  18. tensorflow rank
  19. 简明 ES6 模块
  20. 洛谷 P4525 & P4526 [模板] 自适应辛普森积分

热门文章

  1. 浅析InnoDB引擎的索引和索引原理
  2. CVE-2017-11882 漏洞分析总结 新手漏洞分析详细教程
  3. Go语言核心36讲(Go语言进阶技术二)--学习笔记
  4. 激活NX窗口的按钮
  5. CQL和SQL的CRUD操作比较
  6. airtext初始化(一)
  7. eclipse javase版安装插件开发web项目
  8. [对对子队]发布声明Beta
  9. Scrum Meeting 0427
  10. Linux基础是零基础必须要过的关,你懂了多少