146. LRU 缓存

这道题只要把双向链表和哈希表连接起来就行,只是写起来有点麻烦了

java 复制代码
class LRUCache {
class DLinkedNode {
        int key;
        int value;
        DLinkedNode prev;
        DLinkedNode next;
        public DLinkedNode(){}
        public DLinkedNode(int _key, int _value) {key = _key;value = _value;}
    }

    private Map<Integer, DLinkedNode> cache = new HashMap<Integer, DLinkedNode>();
    private int size;
    private int capacity;
    private DLinkedNode head, tail;

    public LRUCache(int capacity) {
        this.size = 0;
        this.capacity = capacity;
        //使用伪头部和伪尾部
        head = new DLinkedNode();
        tail = new DLinkedNode();
        //双向链表
        head.next = tail;
        tail.prev = head;
    }
    
    public int get(int key) {
        DLinkedNode node = cache.get(key);   
        if(node == null){
            return -1;
        }
        //如果节点存在
        moveToHead(node);
        return node.value;
    }
    
    public void put(int key, int value) {
        //尝试从原来的双向哈希链表中获取
        DLinkedNode node = cache.get(key);
        if(node == null){
            //不存在创建新节点
            DLinkedNode newNode = new DLinkedNode(key, value);
            //添加进表
            cache.put(key,newNode);
            //添加到双向链表头部
            addToHead(newNode);
            ++size;
            if (size > capacity){
                //超出容量,删除尾部元素
                DLinkedNode tail = removeTail();
                //删除哈希表中对应项
                cache.remove(tail.key);
                --size;
            }
        }
        //存在节点,先定位再修改数据,并移到头部
        else{
            node.value = value;
            moveToHead(node);
        }

    }
    private void moveToHead(DLinkedNode node){
        removeNode(node);
        addToHead(node);
    }
    private void removeNode(DLinkedNode node){
        node.prev.next = node.next;
        node.next.prev = node.prev;
    }
    private void addToHead(DLinkedNode node){
        node.prev = head;
        node.next = head.next;f
        head.next.prev = node;
        head.next = node;
    }
    private DLinkedNode removeTail(){
        DLinkedNode res = tail.prev;
        removeNode(res);
        return res;
    }
}

/**
 * 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 小时前
DevDocQA 的 Redis 答案缓存实战:版本切换、异步写回与故障排查
数据库·redis·缓存
captain3767 小时前
Redis简单使用
数据库·redis·缓存
黄俊懿10 小时前
【架构师从入门到进阶】第七章:缓存和存储的优化思路——第六节:数据库更新和缓存更新的组合方案
数据库·缓存·性能优化·架构·系统架构
TDengine (老段)1 天前
TDengine 读缓存与 LAST 查询完全指南
大数据·数据库·物联网·缓存·时序数据库·tdengine·涛思数据
钉先生贾1 天前
HarmonyOS 7.0 多设备协同实战 35:用户撤销分享链接后,怎样使缓存入口同时失效
缓存·华为·harmonyos
FungLeo1 天前
FengCMS ·PHP+MySQL升级实录 · 富文本清洗配置实战:白名单、URI scheme、禁 iframe/id、实例缓存
mysql·缓存·php·xss·fengcms
Flynn Liang2 天前
AI Agent Redis 生产踩坑实录:13 类故障的根因、复现与调优
数据库·人工智能·redis·缓存·ai agent·故障分析
FungLeo2 天前
FengCMS · PHP+MySQL 升级实录· 编译缓存并发:锁定产物执行、避免重复发布与三类缓存清理
mysql·缓存·php·并发·fengcms
Flynn Liang2 天前
RAG 检索性能提升:Redis 向量 + 全文 + 元数据过滤的混合检索实战
数据库·人工智能·redis·缓存·rag·ai agent·智能体
FungLeo2 天前
FengCMS· PHP+MySQL 升级实录 ·模板编译器原理:{$var} 怎么编译成 PHP、缓存怎么失效
mysql·缓存·php·模板引擎·fengcms