LeetCode 刷题【146. LRU 缓存】

146. LRU 缓存

自己做

解:集合哈希

java 复制代码
class LRUCache {
    Map<Integer, Integer> data;
    Map<Integer, Integer> key_time; //根据key查询时间
    Map<Integer, Integer> time_key; //根据时间查询key
    int now_time = 0;               //当前时间
    int len = 0;                    //最大数据存放量

    public LRUCache(int capacity) {
        data = new HashMap();
        key_time = new HashMap();
        time_key = new TreeMap();
        len = capacity;
    }
    
    public int get(int key) {
        if(data.containsKey(key)){                  //找到数据
            time_key.remove(key_time.get(key));     //更新访问时间
            time_key.put(now_time, key);
            key_time.put(key, now_time++);      

            //返回数据
            return data.get(key);
        }

        return -1;
        
    }
    
    public void put(int key, int value) {
        if(data.containsKey(key)){                  //判断数据原先是否存在
            data.put(key, value);                   //更新数据
            time_key.remove(key_time.get(key));     //更新原本的时间
            time_key.put(now_time, key);
            key_time.put(key, now_time++);
        }
        else{                                       //正常添加
            if(data.size() == len){                 //队满了
                Map.Entry<Integer, Integer> firstEntry = time_key.entrySet().iterator().next();     //淘汰最早的(时间最前的)
                int k = firstEntry.getValue();

                time_key.remove(key_time.get(k));
                key_time.remove(k);
                data.remove(k);
            }

            data.put(key, value);
            key_time.put(key, now_time);
            time_key.put(now_time++, key);
        }

    }
}

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

看题解

官方题解:双向链表+哈希

java 复制代码
public 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;
        }
        // 如果 key 存在,先通过哈希表定位,再移到头部
        moveToHead(node);
        return node.value;
    }

    public void put(int key, int value) {
        DLinkedNode node = cache.get(key);
        if (node == null) {
            // 如果 key 不存在,创建一个新的节点
            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 {
            // 如果 key 存在,先通过哈希表定位,再修改 value,并移到头部
            node.value = value;
            moveToHead(node);
        }
    }

    private void addToHead(DLinkedNode node) {
        node.prev = head;
        node.next = head.next;
        head.next.prev = node;
        head.next = node;
    }

    private void removeNode(DLinkedNode node) {
        node.prev.next = node.next;
        node.next.prev = node.prev;
    }

    private void moveToHead(DLinkedNode node) {
        removeNode(node);
        addToHead(node);
    }

    private DLinkedNode removeTail() {
        DLinkedNode res = tail.prev;
        removeNode(res);
        return res;
    }
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/lru-cache/solutions/259678/lruhuan-cun-ji-zhi-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
2601_961194025 分钟前
考研专业课在哪里参加考试|考点|流程|资料已整理
linux·考研·ubuntu·缓存·centos·负载均衡
闪电悠米7 分钟前
黑马点评-Redis 消息队列-01_why_redis_mq
java·数据库·spring boot·redis·缓存·junit·消息队列
IT策士8 分钟前
Redis 从入门到精通:初识 Redis
数据库·redis·缓存
IT策士8 分钟前
Redis 从入门到精通:数据结构Hash 与 List
数据结构·redis·哈希算法
开源Z29 分钟前
LeetCode 238 · 除自身以外数组的乘积:左右两遍扫描,不用除法
算法·leetcode
8Qi81 小时前
LeetCode 5:最长回文子串(Longest Palindromic Substring)—— 题解
算法·leetcode·职场和发展·动态规划
Micro麦可乐9 小时前
Spring Boot 实战:从零设计一个短链系统(含完整代码与数据库设计)
数据库·spring boot·后端·哈希算法·雪花算法·短链系统
如竟没有火炬11 小时前
最大矩阵——单调栈
数据结构·python·线性代数·算法·leetcode·矩阵
8Qi812 小时前
LeetCode 1143 & 718:最长公共子序列 / 最长重复子数组
算法·leetcode·职场和发展·动态规划
电报号dapp11912 小时前
DApp经济模型设计:2026年反泡沫完全指南
区块链·智能合约·哈希算法