剑指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); // 将新结点添加到链表末尾
    }
};
相关推荐
lzptouch13 分钟前
线性回归算法
算法·回归·线性回归
专注前端30年19 分钟前
【JavaScript】reduce 方法的详解与实战
开发语言·前端·javascript
曹牧1 小时前
C#:数组不能使用Const修饰符
java·数据结构·算法
派大星爱吃猫1 小时前
希尔排序详解
算法·排序算法·希尔排序
专注前端30年2 小时前
2025 最新 Vue2/Vue3 高频面试题(10月最新版)
前端·javascript·vue.js·面试
Highcharts.js2 小时前
选择合适的组合:如何打造数据可视化的“黄金组合”
javascript·信息可视化·highcharts·交互式图表开发
kkkkk0211062 小时前
【Rust创作】Rust 错误处理:从 panic 到优雅控制
开发语言·算法·rust
爱学习的小鱼gogo2 小时前
pyhton 螺旋矩阵(指针-矩阵-中等)含源码(二十六)
python·算法·矩阵·指针·经验·二维数组·逆序
angelQ3 小时前
Vue 3 中 ref 获取 scrollHeight 属性为 undefined 问题定位
前端·javascript
坚持编程的菜鸟3 小时前
LeetCode每日一题——二进制求和
c语言·算法·leetcode