146. LRU 缓存

复制代码
https://leetcode.cn/problems/lru-cache/description/?envType=study-plan-v2&envId=top-100-liked
复制代码
最近最久未使用,显然我们需要维护一个使用队列,最近使用过的在队尾,未使用过的靠近队首
并且他要求函数 get 必须以 O(1) 的平均时间复杂度运行显然我们需要用到hash
put 必须以 O(1)的平均时间复杂度运行显然只有链表能做到,并且我们选择双向链表实现
为什么用双向链表而不是单向链表?
将某个节点移动到链表头部或者将链表尾部节点删去,都要用到删除链表中某个节点这个操作。你想要删除链表中的某个节点,需要找到该节点的前驱节点和后继节点。对于寻找后继节点,单向链表和双向链表都能通过 next 指针在O(1)时间内完成;对于寻找前驱节点,单向链表需要从头开始找,也就是要O(n)时间,双向链表可以通过前向指针直接找到,需要O(1)时间。综上,要想在O(1)时间内完成该操作,当然需要双向链表.
java 复制代码
public class LRUCache {
    class Node{
        int key;
        int value;
        Node prev;
        Node next;
        public Node(int key, int value) {
            this.key = key;
            this.value = value;
            this.prev = null;
            this.next = null;
        }
    }
    int capacity;//容量,最大可以存储多少个节点
    int currSize;//当前存储的节点数
    Node head, tail;//头节点,尾节点
    Map<Integer, Node> map = new HashMap<>();//key-value

    public LRUCache(int capacity) {
        this.capacity = capacity;
    }

    public int get(int key) {
        if(map.containsKey(key)){
            Node node = map.get(key);
            removeNode(node);
            return node.value;
        }
        return -1;
    }

    public void put(int key, int value) {
        if(map.containsKey(key)){
            Node node = map.get(key);
            node.value = value;
            removeNode(node);
        } else {
            if(currSize == capacity){
                map.remove(head.key);
                removeNode(head);
                /*if(capacity == 1) {//当前只有一个节点
                    head.key = key;
                    head.value = value;
                } else {//当前节点不是尾节点,则他会被放到尾节点
                    tail.key = key;
                    tail.value = value;
                }*/
                tail.key = key;
                tail.value = value;
                map.put(key, tail);
            } else if(currSize < capacity){
                Node node = new Node(key, value);
                map.put(key, node);
                if(currSize == 0) {//当前没有节点
                    head = node;
                    tail = node;
                } else {
                    tail.next = node;
                    node.prev = tail;
                    tail = node;
                }
                currSize++;
            }

        }
    }

    //将节点node移动到链表尾
    public void removeNode(Node node){
        if(node.next == null) return;
        if(node.prev == null) {//当前节点是头节点
            head = node.next;
            node.next.prev = null;
            node.next = null;
            tail.next = node;
            node.prev = tail;
            tail = node;
        } else {//当前节点不是头节点和尾节点
            node.prev.next = node.next;
            node.next.prev = node.prev;
            node.next = null;
            node.prev = tail;
            tail.next = node;
            tail = node;
        }
    }

    public static void main(String[] args) {
        String[] str = {"LRUCache","put","get","put","get","get"};
        int[][] arr = {{1},{2,1},{2},{3,2},{2},{3}};
        LRUCache lruCache = null;
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i < str.length; i++) {
            if(str[i].equals("LRUCache")) {
                lruCache = new LRUCache(arr[i][0]);
            }
            if(str[i].equals("put")) {
                lruCache.put(arr[i][0], arr[i][1]);
            }
            if(str[i].equals("get")) {
                list.add(lruCache.get(arr[i][0]));
                continue;
            }
            list.add(null);
        }
        System.out.println(list);
    }
}
相关推荐
还是鼠鼠27 分钟前
tlias智能学习辅助系统--Maven 高级-私服介绍与资源上传下载
java·spring boot·后端·spring·maven
Xiaokai丶1 小时前
Java 8 新特性深度剖析:核心要点与代码实战
java
灵魂猎手1 小时前
3. MyBatis Executor:SQL 执行的核心引擎
java·后端·源码
Galaxy在掘金1 小时前
从业8年,谈谈我认知的后端架构之路-1
java·架构
努力努力再努力wz2 小时前
【c++深入系列】:万字详解模版(下)
java·c++·redis
楽码2 小时前
底层技术SwissTable的实现对比
数据结构·后端·算法
2301_793086872 小时前
Redis 04 Reactor
数据库·redis·缓存
瓦特what?3 小时前
关于C++的#include的超超超详细讲解
java·开发语言·数据结构·c++·算法·信息可视化·数据挖掘
是乐谷3 小时前
阿里云杭州 AI 产品法务岗位信息分享(2025 年 8 月)
java·人工智能·阿里云·面试·职场和发展·机器人·云计算
重生之我是Java开发战士4 小时前
【数据结构】深入理解单链表与通讯录项目实现
数据结构·链表