剑指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); // 将新结点添加到链表末尾
    }
};
相关推荐
十五_在努力2 分钟前
参透 JavaScript —— 彻底理解 new 操作符及手写实现
前端·javascript
恣艺5 分钟前
LeetCode 124:二叉树中的最大路径和
算法·leetcode·职场和发展
猫头虎11 分钟前
如何在 macOS 上使用 dnsmasq 搭建本地 DNS 缓存/转发
开发语言·macos·缓存·golang·beautifulsoup·beego·go1.19
典学长编程15 分钟前
前端开发(HTML,CSS,VUE,JS)从入门到精通!第四天(DOM编程和AJAX异步交互)
javascript·css·ajax·html·dom编程·异步交互
weisian15122 分钟前
力扣经典算法篇-42-矩阵置零(辅助数组标记法,使用两个标记变量)
算法·leetcode·矩阵
lwlcode30 分钟前
前端大数据渲染性能优化 - 分时函数的封装
前端·javascript
恣艺35 分钟前
LeetCode 123:买卖股票的最佳时机 III
算法·leetcode·职场和发展
geoyster1 小时前
20250802-102508010-CP
算法
前端Hardy1 小时前
HTML&CSS:超丝滑抛物线飞入购物车效果
前端·javascript·css
VisuperviReborn1 小时前
打造自己的前端监控---前端错误监控
前端·javascript·vue.js