剑指Offer|LCR 031. LRU 缓存

LCR 031. LRU 缓存

运用所掌握的数据结构,设计和实现一个 LRU (Least Recently Used,最近最少使用) 缓存机制

实现 LRUCache 类:

  • LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存
  • int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1
  • void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。

示例:

复制代码
输入
["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 * 105getput

法1:

分析:

js 复制代码
class ListNode {
    constructor(key, value) {
        this.key = key;
        this.value = value;
        this.next = null;
        this.prev = null;
    }
}

var LRUCache = function(capacity) {
    this.capacity = capacity;
    this.cache = new Map(); // 用Map存储 key 和 value
    this.listHead = new ListNode();  // 伪头结点
    this.listTail = new ListNode();  // 伪尾结点
    this.listHead.next = this.listTail
    this.listTail.next = this.listHead
};

// 将节点移到链表末尾
LRUCache.prototype.moveToTail = function(node) {
    this.removeNode(node);
    this.addToTail(node);
};

// 从链表中移除节点
LRUCache.prototype.removeNode = function(node) {
    node.prev.next = node.next;
    node.next.prev = node.prev;
};

// 将节点添加到链表末尾
LRUCache.prototype.addToTail = function(node) {
    // 确保 this.listTail.prev 已经指向有效的节点
    if (this.listTail.prev === null) {
        // 这表明链表为空,初始化头尾节点
        this.listTail.prev = this.listHead;
        this.listHead.next = this.listTail;
    }

    // 插入新的节点
    node.prev = this.listTail.prev;
    node.next = this.listTail;
    this.listTail.prev.next = node;
    this.listTail.prev = node;
};

LRUCache.prototype.get = function(key) {
    if(!this.cache.has(key)) return -1; // 如果没有该 key ,返回 -1
    const node = this.cache.get(key);
    this.moveToTail(node); // 访问后将其移到链表末尾
    return node.value;
};

LRUCache.prototype.put = function(key, value) {
    if(this.cache.has(key)){
        const node = this.cache.get(key);
        node.value = value; // 更新值
        this.moveToTail(node); // 移动到末尾
    } else{
        if(this.cache.size >= this.capacity){
            const nodeToDelete = this.listHead.next;
            this.cache.delete(nodeToDelete.key);
            this.removeNode(nodeToDelete); // 删除最旧的结点
        }
        const newNode = new ListNode(key,value);
        this.cache.set(key, newNode);
        this.addToTail(newNode); // 将新结点添加到链表末尾
    }
};
相关推荐
沐苏瑶1 天前
Java据结构深度解析:AVL 树与红黑树
数据结构·算法
feifeigo1231 天前
MATLAB中对转子建立有限元模型并进行动力学计算
算法
网域小星球1 天前
C 语言从 0 入门(十九)|共用体与枚举:自定义类型进阶
c语言·开发语言·算法·枚举·自定义类型·共用体
灯光设计师1 天前
JavaScript判断受访域名,调用不同的js文件
javascript
聊聊MES那点事1 天前
报表控件Stimulsoft Reports.NET使用教程:发票报告设计
前端·javascript·html·报表工具
favour_you___1 天前
算法练习2026/4/13
算法·深度优先
杰克尼1 天前
redis(day06-多级缓存)
redis·分布式·缓存
SPC的存折1 天前
分布式(加一键部署脚本)LNMP-Redis-Discuz5.0部署指南-小白详细版
linux·运维·服务器·数据库·redis·分布式·缓存
吃着火锅x唱着歌1 天前
LeetCode 1963 使字符串平衡的最小交换次数
算法·leetcode·职场和发展
脑子加油站1 天前
Redis数据库基础
数据库·redis·缓存