前端面试中,手写LRU缓存的出题概率特别大,在自己秋招面试和别人的面试经历中,有很多人都提到了这个,所以我今天就结合leetcode上这题,来自己复习手写一下。
leetcode这一题的链接:leetcode.cn/problems/lr…
题目:请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现 LRUCache 类:
LRUCache(int capacity)以 正整数 作为容量capacity初始化 LRU 缓存int get(int key)如果关键字key存在于缓存中,则返回关键字的值,否则返回-1。void put(int key, int value)如果关键字key已经存在,则变更其数据值value;如果不存在,则向缓存中插入该组key-value。如果插入操作导致关键字数量超过capacity,则应该 逐出 最久未使用的关键字。
函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。
示例:
输入
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]
解释
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1); // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
lRUCache.get(2); // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1); // 返回 -1 (未找到)
lRUCache.get(3); // 返回 3
lRUCache.get(4); // 返回 4
使用双向链表和哈希表
双向链表的节点存放key和对应的value
哈希表用来快速访问存储于双向链表的数据
我们把答案写在下面,并尽可能详细的解释一下
// 定义 ListNode 节点
class ListNode {
constructor (key, value) {
this.key = key
this.value = value
this.next = null
this.prev = null
}
}
// 定义LRUCache
class LRUCache {
constructor(capacity) {
// 容量
this.capacity = capacity
// 哈希表
this.hash = {}
// 缓存数目
this.count = 0
// 虚拟头节点
this.dummyHead = new ListNode()
// 虚拟尾节点
this.dummyTail = new ListNode()
// 将虚拟节点头尾相连
this.dummyHead.next = this.dummyTail
this.dummyTail.prev = this.dummyHead
}
// get方法
get (key) {
// 从哈希表中获取对应的节点
let node = this.hash[key]
// 如果节点不存在,返回-1
if (node == null) return -1
// 被读取了,就把该节点移动到链表头部
this.moveToHead(node)
// 返回该节点的值
return node.value
}
// put方法
put(key, value) {
// 获取链表中的node
let node = this.hash[key]
// 如果不存在,就说明是新数据
if (node == null) {
// 再去判断股容量是否已满
if (this.count == this.capacity) {
// 如果满了,就删除最久没有使用过的数据
this.removeLRUItem()
}
// 如果没有满,就创建新的节点
let newNode = new ListNode(key, value)
// 存入哈希表中
this.hash[key] = newNode
// 将节点添加到链表头部 (因为是最新操作的节点)
this.addToHead(newNode)
// 缓存数目 +1
this.count++
} else {
// 已经存在于链表中,就说明是老数据,更新value
node.value = value
// 将节点移到链表头部
this.moveToHead(node)
}
}
// moveToHead方法
moveToHead(node) {
// 先从链表中删除
this.removeFromList(node)
// 再加到链表的头部
this.addToHead(node)
}
// removeFromList方法
removeFromList(node) {
// 暂存它的后继节点
let temp1 = node.prev
// 暂存它的前驱节点
let temp2 = node.next
// 前驱节点的next指向后继节点,后继节点的prev指向前驱节点
temp1.next = temp2
temp2.prev = temp1
}
// addToHead方法,插入到虚拟头节点和真实头节点之间
addToHead(node) {
// node的prev指向虚拟头节点
node.prev = this.dummyHead
// node的next指向原来真实的头节点
node.next = this.dummyHead.next
//原来的真实头节点的prev指向node
this.dummyHead.next.prev = node
// 虚拟头节点的next指向node
this.dummyHead.next = node
}
// removeLRUItem方法,删除长时间没操作的节点
removeLRUItem () {
//将它从链表尾部删除
let tail = this.popTail()
// 哈希表中也将它删除
delete this.hash[tail.key]
// 缓存数目-1
this.count--
}
// popTail方法, 删除链尾节点
popTail() {
// 通过虚拟尾节点找到他
let tail = this.dummyTail.prev
// 删除该真实节点
this.removeFromList(tail)
// 返回被删除的节点
return tail
}
}
可用前面的题目信息来验证
如果有不对的地方,也希望大家能出来指正!