Leetcode 146 LRU 缓存机制 C++

list::splice()函数详解见:https://blog.csdn.net/justdoithai/article/details/51583775

class LRUCache {
public:
    LRUCache(int capacity) {
        cap=capacity;
    }
    
    int get(int key) {
        auto it=m.find(key);
        if(it==m.end()) return -1;
        l.splice(l.begin(),l,it->second);
        return it->second->second;
    }
    
    void put(int key, int value) {
        auto it=m.find(key);
        if(it!=m.end()) l.erase(it->second);
        l.push_front(make_pair(key,value));
         m[key]=l.begin();
        if(m.size()>cap)
        {
            int k=l.rbegin()->first;
            l.pop_back();
            m.erase(k);
        }
    }
    private:
    int cap;
    list > l;
    unordered_map >::iterator> m;
};

/**
 * 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);
 */

你可能感兴趣的:(Leetcode 146 LRU 缓存机制 C++)