剑指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); // 将新结点添加到链表末尾
    }
};
相关推荐
摘星编程28 分钟前
OpenHarmony + RN:Placeholder文本占位
javascript·react native·react.js
一匹电信狗1 小时前
【LeetCode_547_990】并查集的应用——省份数量 + 等式方程的可满足性
c++·算法·leetcode·职场和发展·stl
鱼跃鹰飞2 小时前
Leetcode会员尊享100题:270.最接近的二叉树值
数据结构·算法·leetcode
三水不滴3 小时前
Redis 过期删除与内存淘汰机制
数据库·经验分享·redis·笔记·后端·缓存
梵刹古音3 小时前
【C语言】 函数基础与定义
c语言·开发语言·算法
筵陌3 小时前
算法:模拟
算法
摘星编程4 小时前
React Native + OpenHarmony:Spinner旋转加载器
javascript·react native·react.js
We་ct4 小时前
LeetCode 205. 同构字符串:解题思路+代码优化全解析
前端·算法·leetcode·typescript
renhongxia14 小时前
AI算法实战:逻辑回归在风控场景中的应用
人工智能·深度学习·算法·机器学习·信息可视化·语言模型·逻辑回归
CoderCodingNo4 小时前
【GESP】C++四级/五级练习题 luogu-P1223 排队接水
开发语言·c++·算法