链表OJ(十六)146. 模拟LRU 缓存 双向链表+哈希

146. LRU 缓存

请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。

实现 LRUCache 类:

  • LRUCache(int capacity)正整数 作为容量 capacity 初始化 LRU 缓存
  • int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1
  • void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。

函数 getput 必须以 O(1) 的平均时间复杂度运行。

示例:

复制代码
输入
["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
cpp 复制代码
struct Node {
    Node* next;            出错点1:把指针定义成了 int*
    Node* pre;
    int key;
    int value;
    Node(int k = 0, int v = 0) {
        key = k;
        value = v;
        next = nullptr;
        pre = nullptr;
    }
};

class LRUCache {
private:
    int capacity;
    Node* dummy = new Node();
    unordered_map<int, Node*> hash_node;

    // 在链表中寻找结点
    Node* Find_Node(int key) {
        if (hash_node.find(key) != hash_node.end()) {
            return hash_node[key];
        } else
            return dummy;
    }

    // 将该cur元素移动至末尾
    void Get_back(Node* cur) 
    {
        cur->pre = dummy->pre;
        dummy->pre->next = cur;
        dummy->pre = cur;
        cur->next = dummy;    
                                            出错点2:忘记给哈希表中进行处理
        // 插入元素的时候也要给哈希表中进行插入    
        hash_node.insert(pair<int, Node*> (cur->key, cur));
    }

    void Delete_Node(Node* cur)
    {
        cur->next->pre = cur->pre;
        cur->pre->next = cur->next;

        // 删除元素的时候也要给哈希表中进行删除
        hash_node.erase(cur->key);
    }

public:
    LRUCache(int c) : capacity(c) {
        dummy->pre = dummy;
        dummy->next = dummy;
    }

    int get(int key) {
        // 1、在双向链表中找到这个元素
        Node* cur = Find_Node(key);
        if (cur != dummy) {
            // 2、更新这对元素到末尾位置
            Delete_Node(cur);
            Get_back(cur);
            return cur->value;              出错点3:指针的访问使用箭头......
        }
        return -1;
    }

    void put(int key, int v) {
        // 1、在双向链表中寻找这个元素
        Node* cur = Find_Node(key);
        
        if (cur != dummy) {
            // 2、如果已经存在,则修改它的value,并尾插
            Delete_Node(cur);
            cur->value = v;
            Get_back(cur);
        }
        else
        {
            // 3、不存在的话,尾插这个新元素对
            cur = new Node(key, v);
            Get_back(cur);
            capacity--;
            if(capacity < 0)
            {
                // 在有新插入情况下,访问容量是否大于capacity,如果超过,头删一个元素。
                Delete_Node(dummy->next);
                capacity++;
            }
        }
    
    }
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache* obj = new LRUCache(capacity);
 * int param_1 = obj->get(key);
 * obj->put(key,value);
 */
相关推荐
CDN36014 小时前
CDN 缓存策略优化实操:出海业务提升缓存命中率,TTL、缓存 Key、刷新配置与踩坑总结
缓存·cdn加速·cdn防护·360cdn
mit6.82420 小时前
保持缓存的有效性
缓存
网安蟹佬霸1 天前
密码学安全实战:从加密原理到哈希破解的完整攻防指南
网络·算法·安全·web安全·开源·密码学·哈希算法
Escalating_xu1 天前
【C++ list 深度解析】从双向循环链表、常用接口与迭代器失效到核心模拟实现
c++·链表·list
AI多Agent协作实战派1 天前
AI多Agent协作系统实战(四十六):改了十次规则,AI员工还是老样子——会话缓存的坑
java·spring·缓存
青 春 记 忆2 天前
LeetCode 142. 环形链表 II|Python 解法详解
python·leetcode·链表
做一个AK梦2 天前
Redis 深入浅出
java·数据库·redis·缓存
Java牛马2 天前
Caffeine 缓存及其应用相关总结
java·redis·缓存·caffeine·数据一致性·本地缓存
xiaojiaohuazi2 天前
国产安陆EG4S20 FPGA实现千兆以太网TCP/IP:AD7606C 8通道1 MSPS采集、SDRAM缓存与Python上位机
tcp/ip·缓存·fpga开发
Nil2082 天前
leetcode 146LRU缓存
算法·leetcode·缓存