146. LRU Cache

来源:互联网 发布:淘宝网小米官方旗舰店 编辑:程序博客网 时间:2024/05/24 01:50

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.

O(n)超时:

 struct cache{        int key;        int value;        int time;    };class LRUCache{public:    vector<cache> cac;    static long long t;    int capacity;    LRUCache(int capacity) {           this->capacity=capacity;           cac.reserve(capacity);    }        int get(int key) {       for (int s=0;s<cac.size();s++)           if (cac[s].key==key)           {               cac[s].time=t++;               return cac[s].value;           }       return -1;    }        void set(int key, int value) {         int flag=0;         int min_key=INT_MAX,c=INT_MAX;         for (int s=0;s<cac.size();s++)         {             if (cac[s].key==key)             {                 cac[s].time=t++;                 cac[s].value=value;                 flag=1;             }             if (c>cac[s].time)             {                 min_key=cac[s].key;                 c=cac[s].time;             }         }         if (cac.size()>=capacity&&!flag)         {               for (int s=0;s<cac.size();s++)               {                   if (cac[s].key==min_key)                   {                       cac.erase(cac.begin()+s);                       break;                   }               }         }         if (!flag&&cac.size()<capacity)         {            cache *pCache=new cache();            pCache->key=key;            pCache->value=value;            pCache->time=t++;            cac.push_back(*pCache);         }    }};long long LRUCache::t=-INT_MAX;

之后我改善后,选择map(红黑树)和list(双向链表)能达到O(logn),map查找速度快O(logn),list插入删除O(1)级别。

访问过的放到链表头部,就达到了LRU的要求,尾部就是不满足条件的。

class LRUCache{public:    list<pair<int,int> > caches;    map<int,list<pair<int,int> >::iterator> mapCache;    int capacity;    LRUCache(int capacity) {           this->capacity=capacity;    }        int get(int key) {        int ret=-1;        map<int,list<pair<int,int> >::iterator>::iterator it=mapCache.find(key);        if (it!=mapCache.end())        {            ret=it->second->second;            pair<int,int> list=*(it->second);            caches.erase(it->second);            caches.push_front(list);            mapCache[key]=caches.begin();        }        return ret;    }        void set(int key, int value) {        map<int,list<pair<int,int> >::iterator>::iterator mIt=mapCache.find(key);        if (mIt!=mapCache.end())        {            pair<int,int> list=*(mIt->second);            list.second=value;            caches.erase(mIt->second);            caches.push_front(list);            mapCache[key]=caches.begin();        }        else        {            pair<int,int> p=make_pair(key,value);            if (mapCache.size()==capacity)            {                mapCache.erase(mapCache.find(caches.back().first));                caches.pop_back();                caches.push_front(p);                mapCache[key]=caches.begin();            }            else            {                caches.push_front(p);                mapCache[key]=caches.begin();            }        }    }};


1 0
原创粉丝点击