题目描述
面试题 16.25. LRU 缓存
难度 中等
设计和构建一个“最近最少使用”缓存,该缓存会删除最近最少使用的项目。缓存应该从键映射到值(允许你插入和检索特定键对应的值),并在初始化时指定最大容量。当缓存被填满时,它应该删除最近最少使用的项目。
它应该支持以下操作: 获取数据 get 和 写入数据 put 。
获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。
示例:
LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // 返回 1
cache.put(3, 3); // 该操作会使得密钥 2 作废
cache.get(2); // 返回 -1 (未找到)
cache.put(4, 4); // 该操作会使得密钥 1 作废
cache.get(1); // 返回 -1 (未找到)
cache.get(3); // 返回 3
cache.get(4); // 返回 4
解析
- 通过采用哈希表+链表的方式,实现查找、插入、删除、移动:O(1)
class ListNode {
constructor(key, value) {
this.key = key;
this.value = value;
this.prev = null;
this.next = null;
}
}
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(key) {
const node = this.hash[key];
if (!node) return -1;
this.moveToHead(node);
return node.value;
}
moveToHead(node) {
// 先将节点从链表中删除
this.removeFromList(node);
// 再添加到链表的头部
this.addToHead(node);
}
removeFromList(node) {
node.prev.next = node.next;
node.next.prev = node.prev;
}
addToHead(node) {
// 先将node节点连接到头节点的后面
// 再将头节点的后面节点连接到node节点
node.prev = this.dummyHead;
node.next = this.dummyHead.next;
this.dummyHead.next.prev = node;
this.dummyHead.next = node;
}
put(key, value) {
const node = this.hash[key];
if (node) {
node.value = value;
this.moveToHead(node);
} else {
const newNode = new ListNode(key, value);
this.hash[key] = newNode;
this.addToHead(newNode);
this.count++;
if (this.count > this.capacity) {
// 删除尾节点
const tail = this.dummyTail.prev;
this.removeFromList(tail);
delete this.hash[tail.key];
this.count--;
}
}
}
}