剑指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); // 将新结点添加到链表末尾
    }
};
相关推荐
先睡42 分钟前
Redis的缓存击穿和缓存雪崩
redis·spring·缓存
Two_brushes.1 小时前
【算法】宽度优先遍历BFS
算法·leetcode·哈希算法·宽度优先
森焱森3 小时前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机
QuantumStack5 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
OEC小胖胖5 小时前
告别 undefined is not a function:TypeScript 前端开发优势与实践指南
前端·javascript·typescript·web
写个博客5 小时前
暑假算法日记第一天
算法
绿皮的猪猪侠5 小时前
算法笔记上机训练实战指南刷题
笔记·算法·pta·上机·浙大
行云&流水6 小时前
Vue3 Lifecycle Hooks
前端·javascript·vue.js
老虎06276 小时前
JavaWeb(苍穹外卖)--学习笔记04(前端:HTML,CSS,JavaScript)
前端·javascript·css·笔记·学习·html
三水气象台6 小时前
用户中心Vue3网页开发(1.0版)
javascript·css·vue.js·typescript·前端框架·html·anti-design-vue