码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 146. LRU 缓存


    1. LRU 缓存

    https://leetcode.cn/problems/lru-cache/description/
    在这里插入图片描述

    2. 代码实现

    在这里插入图片描述

    class LRUCache {
        int capacity;
        HashMap<Integer,Node> table;
        LinkedList<Integer> queue;
        Node head,tail;
        int size=0;
        public LRUCache(int capacity) {
            this.capacity=capacity;
            this.table=new HashMap<>();
            this.queue=new LinkedList<>();
            this.head=new Node();
            this.tail=new Node();
            this.head.next=this.tail;
            this.tail.pre=this.head;
        }
        
        public int get(int key) {
            if(!table.containsKey(key)){
                return -1;
            }
            // 返回该节点对应值
            Node node=table.get(key);
            int val=node.val;
            // 删除该节点
            removeNode(node);
            // 将该节点插入到队列末尾
            addToTail(node);
            return val;
        }
        
        // 从队列中移除节点
        public void removeNode(Node node){
            size--;
            Node left=node.pre,right=node.next;
            left.next=right;
            right.pre=left;
        }
    
        // 将节点添加到队列末尾
        public void addToTail(Node node){
            size++;
            Node last=tail.pre;
            last.next=node;
            node.pre=last;
            node.next=tail;
            tail.pre=node;
        }
    
        public void put(int key, int value) {
            // 查看该值是否存在
            if(get(key)!=-1){
                // 更新
                table.get(key).val=value;
                return;
            }
            // 判断容量是否超过阈值
            Node node=new Node(key,value);
            table.put(key,node);
            // System.out.println(size+" "+capacity);
            if(size==capacity){
                // 如果超过则删除最近最少 使用的节点
                int k=head.next.key;
                removeNode(head.next);
                table.remove(k);
            }
            // 将新的节点插入到队列末尾
            addToTail(node);
            // printList(head);
        }
    
        void printList(Node node){
            Node temp=head.next;
            while(temp!=tail){
                System.out.print(temp.val+" ");
                temp=temp.next;
            }
            System.out.println();
        }
    
        class Node{
            int key;
            int val;
            Node pre;
            Node next;
            public Node(){
            }
    
            public Node(int key,int val){
                this.key=key;
                this.val=val;
            }
        }
    }
    
    /**
     * 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);
     */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
  • 相关阅读:
    设计模式——迭代器模式
    redis 重启后数据丢失
    软考高级系统架构设计师系列之:详细快速掌握软考高级架构设计师考试全部知识点和典型例题上篇
    flink 流计算一条一条处理日志
    侯捷 - C++ Startup 揭密:C++ 程序的生前和死后 (一)
    centos 7 安装tomcat开启apr并应用于springboot
    C#实现一个万物皆可排序的队列
    生命科学领域下的“全球突破性十大技术”干货与分享
    Spring相关
    CentOS 8里的这个功能,天翼云SFS弹性文件校准了
  • 原文地址:https://blog.csdn.net/qq_43665602/article/details/133276674
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号