【C++】每日一题 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 ,则应该 逐出 最久未使用的关键字。

函数 get 和 put 必须以 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

336ms

cpp 复制代码
typedef struct LRUnode{
        int key, value;
        struct LRUnode* prev;
        struct LRUnode* next;
        LRUnode():key(0),value(0),prev(NULL),next(NULL){};
        LRUnode(int key, int value):key(key),value(value),prev(NULL),next(NULL){};
}LRUnode;

class LRUCache {
private:
    unordered_map<int,LRUnode*> m;
    LRUnode *head;
    LRUnode *tail;
    int size;
    int capacity;

public:
    LRUCache(int capacity):capacity(capacity),size(0) {
        head = new LRUnode();
        tail = new LRUnode();
        head->next = tail;
        tail->prev = head;
    }
    
    int get(int key) {     
        int ret;
        auto it = m.find(key);
        if(it != m.end()){          
            ret = it->second->value;
            it->second->next->prev = it->second->prev;
            it->second->prev->next = it->second->next;
            it->second->next=head->next;
            it->second->prev = head;
            head->next->prev = it->second;
            head->next = it->second;
        }
        else{
            ret = -1;
        }
        return ret;
    }
    
    void put(int key, int value) { 
        auto it = m.find(key);
        if(it!=m.end()){
            it->second->value = value;
            it->second->next->prev = it->second->prev;
            it->second->prev->next = it->second->next;
            it->second->next=head->next;
            it->second->prev = head;
            head->next->prev = it->second;
            head->next = it->second;
        }
        else{
            LRUnode *newNode = new LRUnode(key,value);
            m.insert(make_pair(key,newNode));
            newNode->next = head->next;
            head->next->prev = newNode;
            newNode->prev = head;
            head->next = newNode;
            size++;
            if(size>capacity){
                LRUnode *delNode = tail->prev;
                //tail->prev->prev = tail;
                tail->prev = tail->prev->prev;
                tail->prev->next = tail;
                size--;
                m.erase(delNode->key);
                delete delNode;
                
                
            }
        }
    }
};
相关推荐
爱上电路设计4 小时前
有趣的算法
开发语言·c++·算法
窜天遁地大吗喽4 小时前
每日一题~ (判断是否是合法的出栈序列)
c++
2401_854391086 小时前
缓存生命周期管理:探索Memcached的过期策略
数据库·缓存·memcached
三两肉6 小时前
如何使用缓存提升SpringBoot性能(EhCache和Redis方式)
spring boot·redis·缓存
JacksonChen_6 小时前
说一下浏览器中的强缓存和协商缓存的区别
缓存
无知的小菜鸡6 小时前
SSM学习4:spring整合mybatis、spring整合Junit
spring·mybatis
yachihaoteng6 小时前
Studying-代码随想录训练营day27| 贪心算法理论基础、455.分发饼干、376.摆动序列、53.最大子序和
c++·算法·leetcode·贪心算法
逸群不凡6 小时前
C++|哈希应用->布隆过滤器
开发语言·数据结构·c++·算法·哈希算法
从后端到QT6 小时前
Qt 基础组件速学 鼠标和键盘事件
c++·qt
quaer7 小时前
使用引用返回类对象本身
开发语言·c++·算法