【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;
                
                
            }
        }
    }
};
相关推荐
crossaspeed1 天前
Java-SpringBoot的启动流程(八股)
java·spring boot·spring
恒者走天下1 天前
研一、大一大二学计算机应该怎么规划
c++
我是一只小青蛙8881 天前
Windows下MATLAB与C++混合编程实战
c++
玖釉-1 天前
[Vulkan 学习之路] 11 - 组装流水线:固定功能阶段 (Fixed Functions)
c++·windows·图形渲染
f狐0狸x1 天前
【C++修炼之路】C++string的用法
开发语言·c++·string
阿豪只会阿巴1 天前
【多喝热水系列】从零开始的ROS2之旅——Day9 初识话题通信:基本命令
c++·笔记·python·ubuntu·ros2
lpfasd1231 天前
springcloud docker 部署问题排查与解决方案
spring·spring cloud·docker
码小猿的CPP工坊1 天前
C++弱引用智能指针std::weak_ptr使用介绍
开发语言·c++
暮色_年华1 天前
随想3:关于语音采集线程 使用 CFS 调度或者 SCHED_FIFO 的思考
c++
Flash.kkl1 天前
Linux——线程的同步和互斥
linux·开发语言·c++