【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;
                
                
            }
        }
    }
};
相关推荐
好教员好5 小时前
【Spring】整合【SpringMVC】
java·spring
Dream it possible!6 小时前
LeetCode 热题 100_字符串解码(71_394_中等_C++)(栈)
c++·算法·leetcode
浪九天6 小时前
Java直通车系列13【Spring MVC】(Spring MVC常用注解)
java·后端·spring
小斌的Debug日记7 小时前
框架基本知识总结 Day16
redis·spring
My Li.7 小时前
c++的介绍
开发语言·c++
m0_748246877 小时前
maven导入spring框架
数据库·spring·maven
食指Shaye8 小时前
Chrome 中清理缓存的方法
前端·chrome·缓存
椰椰椰耶8 小时前
【redis】全局命令set、get、keys
数据库·redis·缓存
月落星还在9 小时前
Redis 内存淘汰策略深度解析
数据库·redis·缓存
邪恶的贝利亚9 小时前
C++之序列容器(vector,list,dueqe)
开发语言·c++