Leetcode 146. LRU 缓存(Medium)

请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。

实现 LRUCache 类:

  • LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存
  • int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
  • void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。

函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。

示例:

复制代码
输入
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]

解释
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1);    // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
lRUCache.get(2);    // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1);    // 返回 -1 (未找到)
lRUCache.get(3);    // 返回 3
lRUCache.get(4);    // 返回 4

思路:先整理思路,题目说*函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。*那就首先想到HashMap,还要面临顺序问题,数组和链表和树等等比较,显然要使用链表,因为使用的时候要升高顺序,无论是数组还是树等,复杂度都超过了O(n)。

简单来说一共分这几种情况,get时,若没有返回-1,若有就进入使用逻辑(使用逻辑就是删除原来位置再把这个节点放到第一位,用头节点实现),然后返回对应的value。put的时候,若该key有值,则进入使用逻辑+hashmap覆盖;若该key没值,则进入添加逻辑(和使用逻辑类似,但没有删除节点操作),再判断有没有超过容器,超过了就删除最后一个节点(用尾节点实现。),然后让现在容器的大小++;

java 复制代码
class LRUCache {

    // 题目中需要创建一个HashMap来存储参数,因为题目要求复杂度为O(1),顺序就需要通过双向链表的节点实现

    // 构造双向链表节点
    class Node {
        int key;
        int value;
        Node prev;
        Node next;
        public Node() {}
        public Node(int key,int value) {
            this.key = key;
            this.value = value;
        }
    }
    private Node head, tail;
    // 由于LRU,是最近使用,所以为了将使用后的节点放到最前面,还需要一个头尾节点。


    private HashMap<Integer, Node> map;

    // 容量大小
    private Integer capacity;

    // 现在的大小
    private Integer nowCap;

 

   
   
    public LRUCache(int capacity) {
        this.capacity = capacity;
        map = new HashMap<>();
        this.nowCap = 0;
        head = new Node();
        tail = new Node();
        head.next = tail;
        tail.prev = head;
    }
    
    public int get(int key) {
        // get算使用,头插入
        Node node = map.get(key);
        if (node == null) return -1; 
        
        // 查不到就实现节点的删除,然后头插入
        node.prev.next = node.next;
        node.next.prev = node.prev;
        
        // 再头部插入
        node.prev = head;
        node.next = head.next;
        head.next.prev = node;
        head.next = node;

        return node.value;
    }
    
    public void put(int key, int value) {
        Node addNode = new Node(key, value);
        // 当现在的大小没超过容器限度值,就可以put
        Node temp = map.get(key);
        if(this.nowCap < this.capacity) {

            if (temp != null) {

                // 删掉该元素
                temp.prev.next = temp.next;
                temp.next.prev = temp.prev;

                temp.value = value;
                // 再从头部插入
                temp.prev = head;
                temp.next = head.next;
                head.next.prev = temp;
                head.next = temp;
                map.put(key, temp);

            } else {
                // 再头部插入
                addNode.prev = head;
                addNode.next = head.next;
                head.next.prev = addNode;
                head.next = addNode;

                // 并添加映射
                map.put(key, addNode);
                this.nowCap++;

            }

        } else {
            // 超过限度值之后就要进行覆盖
            // 先检查一些能不能查到
            if (temp != null) {

                // 删掉该元素
                temp.prev.next = temp.next;
                temp.next.prev = temp.prev;

                temp.value = value;
                // 再从头部插入
                temp.prev = head;
                temp.next = head.next;
                head.next.prev = temp;
                head.next = temp;
                map.put(key, temp);

            } else {
                // 删除map内的映射
                map.remove(tail.prev.key);
                // 查不到就找出末尾的删除,然后头插入
                tail.prev.prev.next = tail.prev.next;
                tail.prev.next.prev = tail.prev.prev;


                // 再头部插入
                addNode.prev = head;
                addNode.next = head.next;
                head.next.prev = addNode;
                head.next = addNode;

                // 并添加映射
                map.put(key, addNode);
            }
        }
    }

}
相关推荐
黄俊懿10 小时前
【架构师从入门到进阶】第七章:缓存和存储的优化思路——第五节:缓存更新机制说明
缓存·性能优化·架构·系统架构
考虑考虑13 小时前
记一次@Async生产事故
spring boot·后端·spring
郝学胜-神的一滴16 小时前
Effective STL 条款 07:指针容器销毁前先删除
开发语言·c++·vscode·leetcode·rpc·visual studio
CoderYanger17 小时前
A.每日一题:2243. 计算字符串的数字和
java·开发语言·程序人生·leetcode·面试·蓝桥杯·学习方法
Navigator_Z20 小时前
LeetCode //C - 1323. Maximum 69 Number
c语言·算法·leetcode
codigger1 天前
如何保证缓存与数据库一致性
数据库·缓存·程序员
游哇游哇游1 天前
Redis 过期删除与内存淘汰源码级详解:惰性+定期删除、近似 LRU/LFU、8 种策略与内存打满排查
数据库·redis·缓存·面试·bootstrap
星落梦生1 天前
苍穹外卖day05——Redis
数据库·redis·缓存
Mr.Java.1 天前
JetCache自定义序列化实战,Kryo/Protostuff编解码器一步接入
java·spring boot·spring
CoderYanger2 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis