• LRU Cache


    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

    get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
    set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

    思路:get函数在Cache查找key的值,如果存在于Cache中,则将该键值移到Cache首位置,并返回值value反之,则返回-1;set(key,value)函数,如果key存在,则更新相应的value把该元素放到最前面。如果不存在,则创建,并放到最前面,如果容器满了,就把最后那个元素去除。从这可以看出,元素访问的先后是有一定的顺序的,我们可以采用map来对元素进行快速查找,然后定位到查找的结点,使用双向链表来进行移动或删除都很方便。这里使用STL中list容器对于移动或删除都比较容易,代码也比较简洁。

    struct CacheNode
    {
        int key;
        int value;
        CacheNode(int k,int v):key(k),value(v){}
    };
    class LRUCache{
    private:
        int size;
        list<CacheNode> cacheList;
        unordered_map<int,list<CacheNode>::iterator > cacheMap;
    public:
        LRUCache(int capacity) {
            this->size=capacity;
        }
        
        int get(int key) {
            if(cacheMap.find(key)!=cacheMap.end())
            {
                list<CacheNode>::iterator iter=cacheMap[key];
                cacheList.splice(cacheList.begin(),cacheList,iter);
                cacheMap[key]=cacheList.begin();
                return cacheList.begin()->value;
            }
            else
                return -1;
        }
        
        void set(int key, int value) {
            if(cacheMap.find(key)==cacheMap.end())
            {
                if(cacheList.size()==size)
                {
                    cacheMap.erase(cacheList.back().key);
                    cacheList.pop_back();
                }
                cacheList.push_front(CacheNode(key,value));
                cacheMap[key]=cacheList.begin();
            }
            else
            {
                list<CacheNode>::iterator iter=cacheMap[key];
                cacheList.splice(cacheList.begin(),cacheList,iter);
                cacheMap[key]=cacheList.begin();
                cacheList.begin()->value=value;
            }
        }
    };
  • 相关阅读:
    Spring Security 4
    JPA查询
    改进的冒泡算法
    Create User
    COM调用 – VB、PB
    J-Link clone问题
    修复山寨版的J-Link
    C# Web版报表
    HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
    eclipse中字母大小写转换快捷键
  • 原文地址:https://www.cnblogs.com/awy-blog/p/3833427.html
Copyright © 2020-2023  润新知