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);
 */
相关推荐
OH_TPC6 小时前
【鸿蒙优选三方库】@react-native-ohos/react-native-fast-image:高性能图片加载与缓存组件
缓存·华为·harmonyos·鸿蒙
liangshanbo12157 小时前
手写 Event Emitter(事件发射器)
缓存
数智启示录7 小时前
Apache Kafka Consumer 扩到 40 个仍不提速:Partition 上限锁死有效并行度 【Kafka合集】
数据库·经验分享·分布式·缓存·面试·kafka·apache
2601_962297258 小时前
python自带缓存lru_cache用法及扩展的使用_python
python·缓存·装饰器·lru_cache·my_cache
xixingzhe210 小时前
SpringBoot 接口缓存实现
spring boot·后端·缓存
莫得感情 o20 小时前
Redis 05 · 持久化:RDB 与 AOF 怎么保证数据不丢
redis·缓存
curd_boy1 天前
【Redis】Redis从缓存到AI向量平台
人工智能·redis·缓存
kiracrimson1 天前
从缓存的角度看链表与线性表的差异
数据结构·链表·缓存
2601_962301011 天前
深入理解缓存(Cache):原理、应用与优化
缓存·计算机原理·优化策略·数据访问·系统性能
数智启示录1 天前
PostgreSQL 计划缓存实战(第 10 篇):预编译 SQL 前五次都快,第六次为什么可能变慢
经验分享·sql·缓存·postgresql·面试