码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 高阶数据结构学习之LRU_Cache


    这里写自定义目录标题

    • 1. 什么是LRU Cache
    • 2. 相关题目
      • 2.1 146. LRU 缓存题目链接
      • 2.2 题目描述
      • 2.2 代码

    1. 什么是LRU Cache

    Cache:通常用于速度不相同的两个介质之间
    例如:磁盘和内存,内存和cpu

    LRU是Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法。
    高效LRU Cache:任意操作都是O(1)

    2. 相关题目

    2.1 146. LRU 缓存题目链接

    链接: 146. LRU 缓存

    2.2 题目描述

    在这里插入图片描述

    2.2 代码

    补充:
    list一些库里面没有size,当访问size的时候,是遍历一遍链表
    所以尽量不要使用list的size.
    splice 将一个迭代器转移到另一个迭代器的前面
    在这里插入图片描述

    class LRUCache {
    public:
        LRUCache(int capacity) 
            :_capacity(capacity)
        {}
        
        //查找
        int get(int key) {
            auto ret=_hashmap.find(key);
            if(ret!=_hashmap.end())
            {
                Liit it = ret->second;
                //splice 将一个迭代器转移到另一个迭代器的前面
                _list.splice(_list.begin(),_list,it);
                return it->second;
            }
            else
            {
                return -1;
            }
        }
        
        void put(int key, int value) {
            auto ret=_hashmap.find(key);
            if(ret==_hashmap.end())
            {
                //判断是否满了
                if(_capacity==_hashmap.size())
                {
                    pair<int,int> back=_list.back();
                    _hashmap.erase(back.first);
                    _list.pop_back();
                }
                _list.push_front(make_pair(key,value));
                _hashmap[key]=_list.begin();
            }
            else
            {
                Liit it = ret->second;
                it->second=value;
                
                _list.splice(_list.begin(),_list,it);
            }
        }
    private:
        typedef list<pair<int, int>>::iterator Liit;
        unordered_map<int, Liit> _hashmap;
        list<pair<int, int>> _list;
        size_t _capacity;
    };
    
    /**
     * 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
  • 相关阅读:
    AI算法工程师的寒冬?我们完全可以反向思维。。。
    Vector底层源码
    在 .NET 7上使用 WASM 和 WASI
    安卓Activity生命周期
    目前很穷,有什么办法能快速挣钱?
    MongoDB聚合运算符:$cosh
    两千字讲明白java中instanceof关键字的使用!
    12--Django-批量插入数据、分页原理、分页器的使用
    【优化模型】报童的诀窍
    Ubuntu 22.04 安装Nvidia显卡驱动、CUDA、cudnn
  • 原文地址:https://blog.csdn.net/sakeww/article/details/126471997
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号