leetcode146.LRU缓存机制

参考:
https://leetcode.com/problems/lru-cache/discuss/46223/O(1)-unordered_map-%2B-list-%2B-splice

用双向链表+哈希表实现

双向链表的节点为插入的元素,实现高效率的删除和移动操作

哈希表key为元素的key,值为元素节点在队列中的指针,实现高效率的查找操作

取元素:
	不存在直接返回,存在时需要将对应节点移动到队列头,然后返回节点的值
放元素:
	该元素之前不存在
		队列没满,将元素插入队列头,同时加入哈希表
		队列满了,删除队尾元素,哈希表也删除对应元素,然后将新元素加入队列头和哈希表中
	该元素之前存在
		更新节点值,将节点移动到队头
class LRUCache {
private:
    // A list of (key, value) pairs
    list<pair<int, int>> items;  //存放节点,每个节点包含key和value,链表头部是最近用的。尾部是最近没用的
    // Map items to iterators (pointers) to list nodes
    unordered_map<int, list<pair<int, int>>::iterator> cache;  //key为元素的键,value为元素节点的指针
    // The capacity of the list
    int capacity;

public:
    LRUCache(int capacity) : capacity(capacity) {}

    int get(int key) {
        // If key is not found in hash map, return -1
        if (cache.find(key) == cache.end())  //不存在
            return -1;
        // Move the (key, value) pair to the beginning of the list
        items.splice(items.begin(), items, cache[key]); //将items里cache[key]所指元素移动到items的头部
        return cache[key]->second;
    }

    void put(int key, int value) {
        // The key is not in the hash table
        if (cache.find(key) == cache.end()) {  //如果要加入的元素之前不存在
            // If the cache is full then delete the least recently
            // used item, which is at the end of the list
            if (items.size() == capacity) {  //如果队列满了
                cache.erase(items.back().first); //删除队尾的元素对应的哈希表中的值
                items.pop_back();   //删除队列中的值
            }
            items.push_front(make_pair(key, value)); //放入队头
            cache[key] = items.begin();  //加入哈希表
        } else {  //要加入的元素之前存在
            // Update the value associated with the key
            cache[key]->second = value;  //更新值
            // Move the (key, value) pair to the beginning of the list
            items.splice(items.begin(), items, cache[key]); //将该元素移动到队头
        }
    }
};

//代码里的splice()函数可以用先删除该元素,再在链表头插入来代替

不用splice函数的做法,其实和上面的方法一样,略看即可
参考:
https://leetcode.com/problems/lru-cache/discuss/45976/C%2B%2B11-code-74ms-Hash-table-%2B-List

class LRUCache {
private:
    int capacity;
    list<int> recent;  //按顺序存放最近用过的元素,队首最近用过,队尾最近没用过
    unordered_map<int, int> cache; //存放插入的节点
    unordered_map<int, list<int>::iterator> pos; //存放节点和其在链表中的位置
    void use(int key) {
        if (pos.find(key) != pos.end()) {
            recent.erase(pos[key]);
        } else if (recent.size() >= capacity) {
            int old = recent.back();  
            recent.pop_back(); //将该节点删除
            cache.erase(old);
            pos.erase(old);
        }
        recent.push_front(key); //将该节点插入队首
        pos[key] = recent.begin();
    }
public:
    LRUCache(int capacity): capacity(capacity) {}
    int get(int key) {
        if (cache.find(key) != cache.end()) {
            use(key);
            return cache[key];
        }
        return -1;
    }
    void set(int key, int value) {
        use(key);
        cache[key] = value;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值