【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;
                
                
            }
        }
    }
};
相关推荐
River41639 分钟前
Javer 学 c++(十三):引用篇
c++·后端
感哥3 小时前
C++ std::set
c++
侃侃_天下4 小时前
最终的信号类
开发语言·c++·算法
麦兜*4 小时前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
博笙困了4 小时前
AcWing学习——差分
c++·算法
麦兜*4 小时前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
青草地溪水旁5 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁5 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
-Xie-5 小时前
Mysql杂志(十六)——缓存池
数据库·mysql·缓存
七夜zippoe5 小时前
缓存与数据库一致性实战手册:从故障修复到架构演进
数据库·缓存·架构