力扣 146. LRU 缓存

64 阅读1分钟

🔗 leetcode.cn/problems/lr…

题目

思路

  • k-v 使用 map 存储
  • 对于 key 的访问,使用 list 存储
  • 每次读写 map,更新访问 key 的 list
  • c++ stl 的 list 就够用了,主要是熟悉其接口

代码

class LRUCache {
public:
    struct item {
        int val;
        list<int>::iterator it;
    };
    unordered_map<int, struct item*> mp;
    list<int> l;
    int size;
    LRUCache(int capacity) {
        size = capacity;
    }
    
    int get(int key) {
        if (mp.count(key) <= 0) return -1;
        int ret = mp[key]->val;
        auto it = mp[key]->it;
        l.erase(it);
        l.push_front(key);
        mp[key]->it = l.begin();
        return ret;
    }
    
    void put(int key, int value) {
        if (mp.count(key) > 0) {
            mp[key]->val = value;
            auto it = mp[key]->it;
            l.erase(it);
            l.push_front(key);
            mp[key]->it = l.begin();
        } else {
            if (size == mp.size()) {
                int old_key = l.back();
                l.pop_back();
                l.push_front(key);
                auto* item = mp[old_key];
                item->val = value;
                item->it = l.begin();
                mp.erase(old_key);
                mp[key] = item;
            } else {
                struct item* pair = new item;
                pair->val = value;
                l.push_front(key);
                pair->it = l.begin();
                mp[key] = pair;
            }
        }
        
    }
};

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