LeetCode: LRU Cache

来源:互联网 发布:2017院士知乎 编辑:程序博客网 时间:2024/05/16 05:28

这个最坑是需求不写明。set的key如果存在的话,value要替换,同时把LRU的位置刷新。


class LRUCache{public:typedef list<int>::iterator ListIt;    LRUCache(int capacity):_capacity(capacity) {        _data.reserve(_capacity+1);     }        int get(int key) {        auto it = _data.find(key);                                                                                                                if (it==_data.end()){                                                                                                                         return -1;                                                                                                                   }                                                                                                                                         _lru.erase(it->second.second);                                                                                                            _lru.push_front(key);                                                                                                                     it->second.second = _lru.begin();                                                                                                         return it->second.first;      }        void set(int key, int value) {        auto it = _data.find(key);                                                                                                                if(it!=_data.end()){            it->second.first = value;            _lru.erase(it->second.second);            _lru.push_front(key);            it->second.second=_lru.begin();            return;                                                                                                                                 }                                                                                                                                                                                                                                                            if(_capacity > 0){                                                                                                                            _capacity --;                                                                                                                           }else{                                                                                                                                   int tmpKey = *(--_lru.end());                                                                                                           _lru.pop_back();                                                                                                                        _data.erase(tmpKey);                                                                                                                    }                                                                                                                                         _lru.push_front(key);                                                                                                                     _data.insert(make_pair(key, make_pair(value, _lru.begin())));    }    list<int> _lru;    int _capacity;    unordered_map<int, pair<int, ListIt>> _data;};

0 0