• LRU缓存


    设计和构建一个“最近最少使用”缓存,该缓存会删除最近最少使用的项目。缓存应该从键映射到值(允许你插入和检索特定键对应的值),并在初始化时指定最大容量。当缓存被填满时,它应该删除最近最少使用的项目。

    它应该支持以下操作: 获取数据 get 和 写入数据 put 。

    获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
    写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。

    示例:

    LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );
    
    cache.put(1, 1);
    cache.put(2, 2);
    cache.get(1);       // 返回  1
    cache.put(3, 3);    // 该操作会使得密钥 2 作废
    cache.get(2);       // 返回 -1 (未找到)
    cache.put(4, 4);    // 该操作会使得密钥 1 作废
    cache.get(1);       // 返回 -1 (未找到)
    cache.get(3);       // 返回  3
    cache.get(4);       // 返回  4

    通过双向链表 + hash表的形式,链表用于储存节点,方便节点之间的移动,最右边存储最久没有使用的,最左边用于存储最新使用的。hash表用于进行定位,便于快速找到对应的值
    class LRUCache {
    public:
        struct node {
            int key;
            int val;
            node* pre;
            node* nex;
            node(){}
            node(int xx, int yy) {
                key = xx;
                val = yy;
                pre = nullptr;
                nex = nullptr;
            }
        };
    
        LRUCache(int capacity) {
            real_size = 0;
            max_size = capacity;
            head = new node(-1, -1);
            tail = new node(-1, -1);
            head->nex = tail;
            head->pre = tail;
            tail->nex = head;
            tail->pre = head;
        }
        
        int get(int key) {
            if(!sto.count(key))
                return -1;
            judge_head(key);
            return sto[key]->val;
        }
        
        void put(int key, int value) {
            if(max_size <= 0)
                return ;
            if(sto.count(key)) {
          /* 可能会更新 */ judge_head(key); sto[key]
    ->val = value; return ; } if(real_size >= max_size) { auto cur = tail->pre; auto prev = cur->pre; tail->pre = prev; prev->nex = tail; cur->nex = nullptr; cur->pre = nullptr; sto.erase(cur->key); //ree(cur); real_size--; } auto cur = new node(key, value); auto next = head->nex; head->nex = cur; next->pre = cur; cur->pre = head; cur->nex = next; sto[key] = cur; real_size++; return ; } void judge_head(int key) { auto cur = sto[key]; auto prev = cur->pre; auto next = cur->nex; prev->nex= next; next->pre = prev; cur->pre = nullptr; cur->nex = nullptr; next = head->nex; head->nex = cur; next->pre = cur; cur->pre = head; cur->nex= next; return ; } private: int real_size; int max_size; node *head; node *tail; unordered_map<int, node*> sto; }; /** * 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); */

     

  • 相关阅读:
    全局临时表的应用 Timothy
    结合windows服务的Socket聊天室 Timothy
    阿拉伯数字转换成金额大写金额(包括小数) Timothy
    回文字符串和栈 Timothy
    SQL 读取不连续的第30到40之间的数据 Timothy
    C#中as和is关键字 Timothy
    嵌套事务和事务保存点的错误处理 Timothy
    隐式事务 Timothy
    float,double和decimal类型 Timothy
    string 值类型还是引用类型 Timothy
  • 原文地址:https://www.cnblogs.com/letlifestop/p/12468497.html
Copyright © 2020-2023  润新知