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);
    }
}
相关推荐
步行cgn6 分钟前
Spring 启动报错:BeanFactory not initialized 的原因与解决
java·python·spring
步行cgn14 分钟前
Spring 报错:No bean class specified on bean definition 的原因与解决
java·后端·spring
玄芯散人1 小时前
【筑基·047】内存Hierarchy:寄存器到硬盘的速度差
缓存·内存·计算机基础
zhangjw341 小时前
第47篇:Java综合实战:电商秒杀系统(高并发场景)
java·开发语言
土司大王1 小时前
LeetCode hot100 回溯专题总结:Java 通用模板、决策树模型
java·算法·leetcode·决策树
Wang's Blog1 小时前
Java框架快速入门:Spring Security+OAuth2之用户注册与唯一性校验实现
java·数据库·spring
Wang's Blog2 小时前
Java框架快速入门: Spring Security+OAuth2之多因子认证与TOTP实战
java·spring·ui
滕州市燕猫虎计算机科技工作室个体工商户2 小时前
Java面试题汇总
java·开发语言
动词ing3 小时前
【学习笔记】数据结构栈与队列(栈先进后出+队列先进先出+括号匹配+消消乐模型)
数据结构·笔记·学习
xhbh6663 小时前
中小企业 Redis 运维,如何安全归档 RDB、AOF 备份文件?
运维·数据库·缓存·数据备份·文件备份·同步备份·号码备份