美文网首页
BM100 设计LRU缓存结构

BM100 设计LRU缓存结构

作者: help_youself | 来源:发表于2022-07-12 11:57 被阅读0次

基本思想:hash结构m_db存储key-value。链表结构m_link存储最近set的key值,key值在链表中的位置越后,key值越新。m_keyLink存储的是key和std::list<int>::iterator(就是key值在m_link对应的位置)。

class Solution {
public:
    Solution(int capacity){
         // write code here
         m_capacity=capacity;
    }

    int get(int key) {
         // write code here
         auto it=m_db.find(key);
         int res=-1;
         if(it!=m_db.end()){
            res=it->second;
            updateLocation(key);
         }
         return res;
    }

    void set(int key, int value){
         // write code here
        auto it=m_db.find(key);
        if(it!=m_db.end()){
            it->second=value;
            updateLocation(key);
        }else{
            if(m_cached<m_capacity){
                insertNewOne(key,value);
            }else{
                deleteOldOne();
                insertNewOne(key,value);
            }
        }

    }
private:
    void updateLocation(const int &key){
        auto map_it=m_keyLink.find(key);
        if(map_it!=m_keyLink.end()){
            auto list_it=map_it->second;
            m_link.erase(list_it);
            auto new_it=m_link.insert(m_link.end(),key);
            map_it->second=new_it;
        }
    }
    void deleteOldOne(){
        if(m_cached>0){
            auto list_it=m_link.begin();
            if(list_it!=m_link.end()){
                int key=(*list_it);
                m_link.erase(list_it);
                auto map_it=m_keyLink.find(key);
                if(map_it!=m_keyLink.end()){
                    m_keyLink.erase(map_it);
                }
                auto db_it=m_db.find(key);
                if(db_it!=m_db.end()){
                    m_db.erase(db_it);
                }
                m_cached--;
            }


        }

    }
    void insertNewOne(const int &key,const int &value){
         m_db.insert(std::make_pair(key,value));
         auto list_it=m_link.insert(m_link.end(),key);
         m_keyLink.insert(std::make_pair(key,list_it));
         m_cached++;
    }
    typedef std::list<int>::iterator Loc;
    std::unordered_map<int,Loc> m_keyLink;
    std::list<int> m_link;
    std::unordered_map<int,int> m_db;
    int m_cached=0;
    int m_capacity=0;

};

相关文章

  • BM100 设计LRU缓存结构

    基本思想:hash结构m_db存储key-value。链表结构m_link存储最近set的key值,key值在链表...

  • 算法笔记——LRU和LFU缓存结构

    LRU缓存结构 问题描述: 设计可以变更的缓存结构(LRU)【题目】设计一种缓存结构,该结构在构造时确定大小,假设...

  • LeetCode-146- LRU 缓存机制

    LRU 缓存机制 题目描述:运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 。实现 ...

  • LeetCode146 动手实现LRU算法

    146. LRU缓存机制 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持...

  • 设计LRU缓存结构

    这道题想了一天,自己做的只通过90%,不知道错在哪里。 答案用了双向链表 get()函数:先用iter =mp[ ...

  • 算法第4天:LRU缓存机制

    leetcode 146. LRU缓存机制 middle 运用你所掌握的数据结构,设计和实现一个 LRU (最...

  • LeetCode热门100题算法和思路(day6)

    LeetCode 146 LRU缓存 题目详情 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) ...

  • 力扣(LeetCode) -146 LRU缓存机制

    本题考察的LRU缓存机制,HashMap和链表 题目描述 运用你所掌握的数据结构,设计和实现一个 LRU (最近...

  • 146. LRU 缓存

    146. LRU 缓存[https://leetcode.cn/problems/lru-cache/] 请你设计...

  • LRU缓存

    请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。实现 LRUCache 类:LRUCac...

网友评论

      本文标题:BM100 设计LRU缓存结构

      本文链接:https://www.haomeiwen.com/subject/ajxabrtx.html