【力扣hot100题】(034)LRU缓存

做完这题已经没有任何力气写链表题了。

思路很简单,就是调试特别的痛苦。

老是频频报错,唉。

cpp 复制代码
class LRUCache {
public:
    struct ListNode{
        int key,val;
        ListNode* next; 
        ListNode* prev;
        ListNode() : key(0), val(0), next(nullptr), prev(nullptr) {}
        ListNode(int key, int value) : key(key), val(value), next(nullptr), prev(nullptr) {}
        ListNode(int key, int value, ListNode* next=nullptr, ListNode* prev=nullptr) : key(key), val(value), next(next), prev(prev) {}
    };
    ListNode* head=new ListNode();
    ListNode* tail=head;
    int capacity;
    map<int,ListNode*> hash;
    LRUCache(int capacity) {
        this->capacity=capacity;
    }
    
    int get(int key) {
        if(hash.find(key)!=hash.end()){
            if(head->next==hash[key]) return hash[key]->val;
            hash[key]->prev->next=hash[key]->next;
            if(hash[key]->next) hash[key]->next->prev=hash[key]->prev;
            if(hash[key]==tail) tail=hash[key]->prev;
            hash[key]->next=head->next;
            if(head->next) head->next->prev=hash[key];
            hash[key]->prev=head;
            head->next=hash[key];
            return hash[key]->val;
        }
        else return -1;
    }
    
    void put(int key, int value) {
        if(hash.find(key)!=hash.end()){
            hash[key]->val=value;
            get(key);
            return;
        }
        else if(capacity==0){
            hash.erase(tail->key);
            tail=tail->prev;
            tail->next=nullptr;
        }
        else capacity--;
        ListNode* newhead=new ListNode(key,value,head->next,head);
        if(tail==head) tail=newhead;
        if(head->next) head->next->prev=newhead;
        head->next=newhead;
        hash[key]=newhead;
    }
};

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

看了答案,其实有更好的做法,可以优化并且降低代码的复杂度,感觉我写代码还是太畏缩了。

优化方案一:将头指针和尾指针都设成虚拟指针,一个指向双向链表头部的前一个位置,一个指向尾部的后一个位置。(我写的时候为了节省空间复杂度只弄了一个头虚拟指针,还是太不敢了)

优化方案二:构造更多的函数,这样就可以直接调用,代码逻辑会清晰很多。

于是按这个改进思路又写了一遍,果然简单多了......

cpp 复制代码
class LRUCache {
public:
    struct ListNode{
        int key,val;
        ListNode* next; 
        ListNode* prev;
        ListNode() : key(0), val(0), next(nullptr), prev(nullptr) {}
        ListNode(int key, int value, ListNode* next=nullptr, ListNode* prev=nullptr) : key(key), val(value), next(next), prev(prev) {}
    };
    ListNode* head;
    ListNode* tail;
    int capacity;
    unordered_map<int,ListNode*> hash;
    LRUCache(int capacity) {
        this->capacity=capacity;
        head=new ListNode();
        tail=new ListNode(0,0,nullptr,head);
        head->next=tail;
    }
    
    int get(int key) {
        if(hash.find(key)==hash.end()) return -1;
        erase(hash[key]);
        insert(hash[key]);
        return hash[key]->val;
    }
    
    void put(int key, int value) {
        if(hash.find(key)!=hash.end()){
            hash[key]->val=value;
            erase(hash[key]);
            insert(hash[key]);
            return ;
        }
        if(capacity>0) capacity--;
        else if(capacity==0){
            hash.erase(tail->prev->key);
            erase(tail->prev);
        }
        ListNode* newnode=new ListNode(key,value,nullptr,nullptr);
        insert(newnode);
        hash[key]=newnode;
    }

    void erase(ListNode* node){
        node->prev->next=node->next;
        node->next->prev=node->prev;
    }
    void insert(ListNode* node){
        head->next->prev=node;
        node->next=head->next;
        node->prev=head;
        head->next=node;
    }
};

/**
 * 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);
 */
相关推荐
元亓亓亓3 分钟前
LeetCode热题100--560.和为K的子数组(前缀和)--中等
算法·leetcode·职场和发展
八股文领域大手子1 小时前
深入浅出限流算法(三):追求极致精确的滑动日志
开发语言·数据结构·算法·leetcode·mybatis·哈希算法
bxlj_jcj2 小时前
如何实现Redis和Mysql中数据双写一致性
redis·缓存·架构
左灯右行的爱情2 小时前
缓存并发更新的挑战
jvm·数据库·redis·后端·缓存
蒟蒻小袁3 小时前
力扣面试150题--K 个一组翻转链表
leetcode·链表·面试
许_安4 小时前
leetcode刷题日记——两数相加
算法·leetcode·职场和发展
Themberfue6 小时前
Redis ⑥-string | hash | list
数据库·redis·分布式·缓存·list
关于不上作者榜就原神启动那件事6 小时前
力扣hot100,739每日温度(单调栈)详解
算法·leetcode·职场和发展
小卡皮巴拉6 小时前
【力扣刷题实战】丢失的数字
c++·算法·leetcode·位运算
傻小胖6 小时前
密码学的hash函数,哈希碰撞, collision resistance, BTC用到的SHA-256简介
区块链·密码学·哈希算法