leetcode146——LRU 缓存机制

在这里插入图片描述

1.数据结构

为了满足O(1)的复杂度,需要哈希表。另外还需要双向链表存放(key,value).
哈希表中存放(key, key在链表中的位置)。

2.算法
2.1 get操作
if key不存在:
	直接返回-1
else 
	在原链表中删除(key,value)(key,value)重新放回链表的头部
	更新哈希表
	返回value
2.2 put操作
if key存在:
	在原链表中删除(key, value)(key,value)重新放回链表的头部
	更新哈希表
else
	if 链表长度达到上限:
		获取链表尾部的key
		在哈希表中删除key
		删除链表尾部元素
		将新的(key,value)插入链表头部
		将(key, key在链表中的位置)放入哈希表
	else
		将新的(key,value)插入链表头部
		将(key, key在链表中的位置)放入哈希表
3.实现

自己实现双向链表,map中存放(key,node)

class LRUCache {
    class DLinkedNode{
        public:
            int key;
            int value;
            DLinkedNode* prev;
            DLinkedNode* next;
    };

    void addNode(DLinkedNode* node){
        //添加结点到链表头部
        node->next = head->next;
        head->next = node;
        node->prev = head;
        node->next->prev = node;
    }

    int removeNode(DLinkedNode* node){
        //删除双向链表中的某一结点
        node->prev->next = node->next;
        node->next->prev = node->prev;
        return node->key;
    }

    void moveToHead(DLinkedNode* node){
        removeNode(node);//先删除
        addNode(node);//再将结点放到链表头部
    }

private:
    unordered_map<int,DLinkedNode*> cache;
    DLinkedNode* head;
    DLinkedNode* tail;
    int size;
    int capacity;
public:
    LRUCache(int capacity) {
        head = new DLinkedNode();
        tail = new DLinkedNode();
        head->next = tail;
        tail->prev = head;

        size = 0;
        this->capacity = capacity;
    }
    
    int get(int key) {
        auto it = cache.find(key);
        if(it == cache.end()){
            return -1;
        }
        moveToHead(it->second);
        return it->second->value;
    }
    
    void put(int key, int value) {
        auto it = cache.find(key);
        if(it == cache.end()){
            //不在cache中
            DLinkedNode* node = new DLinkedNode();
            node->key = key;
            node->value = value;
            addNode(node);
            cache.insert(make_pair(key,node));
            ++size;
            if(size > capacity){
                int k = removeNode(tail->prev);
                cache.erase(k);
                --size;
            }
        }
        else{
            //在cache中
            it->second->value = value;
            moveToHead(it->second);
        }
    }
};

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

或者使用库中的list,map中存放(key,迭代器)

class LRUCache {
private:
    int capacity;
    list<pair<int,int>> cacheList;//key,value
    unordered_map<int,list<pair<int,int>>::iterator> map;//key, key在链表中的迭代器
public:
    LRUCache(int capacity) {
        this->capacity = capacity;
    }
    
    int get(int key) {
        auto it = map.find(key);
        if(it == map.end())
            return -1;//没找到直接返回-1

        auto kv = *map[key];//原来的(key,value)
        cacheList.erase(map[key]);//删除原来的
        cacheList.push_front(kv);//放到头部
        map[key] = cacheList.begin();
        return kv.second;
    }
    
    void put(int key, int value) {
        auto it = map.find(key);
        if(it != map.end()){    //原来有key
            cacheList.erase(map[key]);//删除原来的
            cacheList.push_front(make_pair(key,value));//放到头部
            map[key] = cacheList.begin();        
        }
        else{   //原来没有key
            if(cacheList.size() == capacity){    //链表长度达到上限
                auto lastKey = cacheList.back().first;
                map.erase(lastKey);
                cacheList.pop_back();
            }
            cacheList.push_front(make_pair(key,value));
            map.insert(make_pair(key,cacheList.begin()));
        }
    }
};

/**
 * 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);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值