创见博客
LRU缓存
七崽爱吃小饼干2026/01/07阅读 0专栏 算法合集

LRU缓存

请你设计并实现一个满足 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) 的平均时间复杂度运行。

示例:

codeType
输入
["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

提示:

  • 1 <= capacity <= 3000
  • 0 <= key <= 10000
  • 0 <= value <= 105
  • 最多调用 2 * 105 次 get 和 put

解法

这题需要哈希表配合双向链表进行解答。双向链表用来按使用的先后顺序来维护节点,哈希表存储节点的引用,使得能根据key快速找到对应节点在链表中的位置。

ts
// 查找和修改需要O(1)的时间复杂度,所以可以用哈希表进行存储
// 关键在于怎么去维护各关键字最近使用的情况
// 各关键字需要按照使用时间(get和put都会更新)进行排序,才能在删除时直接删除最旧关键字从而达到O(1)时间复杂度
// 我们可以用一个双向链表(因为双向链表可以快速实现自删)来维护关键字的使用时间
// 并且我们将每个关键字的节点存储到哈希表,这样就可以快速找到对应的节点,然后放到头节点。
// 我们同时维护一个尾节点,删除时只需要删除最后一个节点即可
class LRUListNode {
  public key: number;
  public val: number;
  public next: LRUListNode | null;
  public prev: LRUListNode | null;

  constructor(key?: number, val?: number, next?: LRUListNode | null, prev?: LRUListNode | null) {
    this.key = key ?? -1;
    this.val = val ?? -1;
    this.next = next ?? null;
    this.prev = prev ?? null;
  }
}

class LRUCache {
  private capacity: number;
  private map: Map<number, LRUListNode>;
  private dummyHead: LRUListNode; // 虚拟头节点(哨兵)
  private dummyTail: LRUListNode; // 虚拟尾节点(哨兵)

  constructor(capacity: number) {
    this.capacity = capacity;
    this.map = new Map<number, LRUListNode>();
    
    // 初始化双向链表:虚拟头和虚拟尾互相指向
    this.dummyHead = new LRUListNode();
    this.dummyTail = new LRUListNode();
    this.dummyHead.next = this.dummyTail;
    this.dummyTail.prev = this.dummyHead;
  }

  // 辅助方法:将节点移动到链表头部(虚拟头之后)
  private moveToHead(node: LRUListNode): void {
    // 1. 先从原位置移除节点
    this.removeNode(node);
    // 2. 插入到虚拟头之后
    this.addToHead(node);
  }

  // 辅助方法:添加节点到链表头部
  private addToHead(node: LRUListNode): void {
    node.prev = this.dummyHead;
    node.next = this.dummyHead.next;
    this.dummyHead.next.prev = node;
    this.dummyHead.next = node;
  }

  // 辅助方法:删除链表中的指定节点
  private removeNode(node: LRUListNode): void {
    node.prev.next = node.next;
    node.next.prev = node.prev;
  }

  // 辅助方法:删除链表尾节点(最久未使用的节点)
  private removeTail(): LRUListNode {
    const tailNode = this.dummyTail.prev; // 真实尾节点是虚拟尾的prev
    this.removeNode(tailNode);
    return tailNode;
  }

  // 查找key对应的value,不存在返回-1,存在则移到头部
  public get(key: number): number {
    if (!this.map.has(key)) {
      return -1;
    }
    const node = this.map.get(key)!; // 非空断言(已判断存在)
    this.moveToHead(node);
    return node.val;
  }

  // 添加/更新key-value,超出容量则删除最久未使用的节点
  public put(key: number, value: number): void {
    if (this.map.has(key)) {
      // 存在该key:更新值并移到头部
      const node = this.map.get(key)!;
      node.val = value;
      this.moveToHead(node);
    } else {
      // 不存在该key:新建节点
      const newNode = new LRUListNode(key, value);
      this.map.set(key, newNode);
      this.addToHead(newNode);

      // 超出容量:删除尾节点
      if (this.map.size > this.capacity) {
        const tailNode = this.removeTail();
        this.map.delete(tailNode.key);
      }
    }
  }
}



/**
 * Your LRUCache object will be instantiated and called as such:
 * var obj = new LRUCache(capacity)
 * var param_1 = obj.get(key)
 * obj.put(key,value)
 */
评论
0/100