LeetCode每日一题5月25日 LeetCode146.LRU缓存机制

问题描述:

运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 

获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
写入数据 put(key, value) - 如果密钥已经存在,则变更其数据值;如果密钥不存在,则插入该组「密钥/数据值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/lru-cache

示例:



LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // 返回  1
cache.put(3, 3);    // 该操作会使得密钥 2 作废
cache.get(2);       // 返回 -1 (未找到)
cache.put(4, 4);    // 该操作会使得密钥 1 作废
cache.get(1);       // 返回 -1 (未找到)
cache.get(3);       // 返回  3
cache.get(4);       // 返回  4

解题思路:

本题首先要清楚什么是LRU缓存:

LRU 的全称是 Least Recently Used。也就是说我们认为最近使用过的数据应该是是「有用的」,很久都没用过的数据应该是无用的,内存满了就优先删那些很久没用过的数据。

这题我是看了大神的解法写的。

构建双向链表和unordered_map

不过该双向链表中有key,value。俩个值。

双链表数据结构如下:

struct Node
{
    int key;
    int value;
    Node* pre;
    Node* next;
    // 构造函数初始化
    Node(int key, int value){
        this->key = key;
        this->value = value;
        pre = nullptr;
        next = nullptr;
    }
};

哈希表定义为unordered_map<int,Node*> map。map中的key和map中的value,也就是Node节点中的key是相等的。

每次实现插入时,先查看map里面是否存在该元素,如果存在该元素,则将该元素移至表头

                             若不存在,且map空间小于额定缓存,直接将该元素插入表头,

                                 不存在,将链表尾端元素移除,该元素插入表头。

首先实现链表节点的插入表头和移出操作,时间复杂度都是O(1)

   void remove(Node* cur){
        if(cur==head) head=cur->next;
        else if(cur == tail) tail=cur->pre;
        else{
            cur->pre->next = cur->next;
            cur->next->pre = cur->pre;
        }
    }
    void setHead(Node* cur){
        cur->next = head;
        if(head!=nullptr){
            head->pre = cur;
        }
        head =cur;
        if(tail==nullptr){
            tail = head;
        }
    }

下面是完整代码,unordered_map<int,Node*> 的查找是基于hashmap的,因此其find函数的时间复杂度为O(1)

// 总的思想就是 哈希双向链表
struct Node
{
    int key;
    int value;
    Node* pre;
    Node* next;
    // 构造函数初始化
    Node(int key, int value){
        this->key = key;
        this->value = value;
        pre = nullptr;
        next = nullptr;
    }
};

class LRUCache {
private:
    int size;
    Node* head;
    Node* tail;
    unordered_map<int,Node*> m;
public:
    LRUCache(int capacity) {
       this->size = capacity;
       head = nullptr;
       tail = nullptr;
    }
    
    int get(int key) {
        if(m.count(key)){
            Node* cur = m[key];
            int value = cur->value;
            remove(cur);
            setHead(cur);
            return value;
        }
        return -1;
    }
    
    void put(int key, int value) {
        if(m.count(key)>0){
            Node* cur = m[key];
            cur->value = value;
            remove(cur);
            setHead(cur);
        }else{
            Node* node = new Node(key,value);
            if(m.size()>=size){
                unordered_map<int,Node*>::iterator it = m.find(tail->key);
                remove(tail);
                m.erase(it);
            }
            setHead(node);
            m[key] = node;
        }
    }
    void remove(Node* cur){
        if(cur==head) head=cur->next;
        else if(cur == tail) tail=cur->pre;
        else{
            cur->pre->next = cur->next;
            cur->next->pre = cur->pre;
        }
    }
    void setHead(Node* cur){
        cur->next = head;
        if(head!=nullptr){
            head->pre = cur;
        }
        head =cur;
        if(tail==nullptr){
            tail = head;
        }
    }
};

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值