146. LRU Cache (M)

关于爬虫源影视资源设置 本文介绍了Omnibox 2.1.3版本的爬虫脚本配置方法,重点演示了如何添加猫眼资源数据源。脚本包含完整的API请求处理、数据格式化、视频详情获取和弹幕匹配等功能模块。通过环境变量配置采集站API地址和弹幕API地址,支持视频列表获取、分类检索、详情查询和播放地址解析等功能。脚本采用模块化设计,包含完善的错误处理和日志记录机制,能够将采集站返回的数据转换为标准格式,并支持批量获取视频详情来丰富数据。同时提供了弹幕匹配功能,可根据视频文件名或播放地址自动匹配相关弹幕资源。 阅读详情

LRU Cache (M)

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

The cache is initialized with a positive capacity.

Follow up:
Could you do both operations in O(1) time complexity?

Example:

LRUCache cache = new LRUCache( 2 /* capacity */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // returns 1
cache.put(3, 3);    // evicts key 2
cache.get(2);       // returns -1 (not found)
cache.put(4, 4);    // evicts key 1
cache.get(1);       // returns -1 (not found)
cache.get(3);       // returns 3
cache.get(4);       // returns 4

题意

实现一个LRU缓存机制:查询时,如果不存在目标key,则返回-1,否则返回key对应的value;插入时,如果缓存已满,则将访问时间最远的(key, value)对删去,存入新的(key, value)对。

思路

主要思想是维护一个类数组结构,越近访问的数据越靠近头部,越远访问的数据越靠近尾部。每次查询到旧值或访问新值时,将该值从原位置取出并插入到头部;需要删除数据时,只要删去尾部的数据。

具体实现使用双向链表+HashMap:双向链表使得删除和移动结点的复杂度为 O ( 1 ) O(1) O(1);HashMap用于创建索引,将key值映射到对应的结点,使结点查询复杂度也为 O ( 1 ) O(1) O(1)。


代码实现

class LRUCache {
    // 双向链表结点
    class Node {
        int key, val;
        Node prev, next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }

    private Node head, tail;
    private int capacity;
    private Map<Integer, Node> hash;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        hash = new HashMap<>();
        // dummy结点可以大大简化步骤
        head = new Node(-1, -1);
        tail = new Node(-1, -1);
        head.next = tail;
    }

    public int get(int key) {
        if (!hash.containsKey(key)) {
            return -1;
        }

        // 查询到存在,则将原结点拎出插入到头结点之后
        Node target = hash.get(key);
        removeFromPos(target);
        moveToFirst(target);
        return target.val;
    }

    public void put(int key, int value) {
        if (!hash.containsKey(key)) {
            // 需要插入新值,则新建结点并将其移动到头结点后
            Node temp = new Node(key, value);
            hash.put(key, temp);
            moveToFirst(temp);
        } else {
            // 更新旧值,并将原结点取出插入到头结点之后
            Node target = hash.get(key);
            target.val = value;
            removeFromPos(target);
            moveToFirst(target);
        }
        
        // 超出容量,则删去尾结点之前的结点
        if (hash.size() == capacity + 1) {
            hash.remove(tail.prev.key);
            removeFromPos(tail.prev);
        }
    }

    // 将目标结点移动到头结点之后
    private void moveToFirst(Node target) {
        target.next = head.next;
        head.next.prev = target;
        target.prev = head;
        head.next = target;
    }

    // 将目标结点从链中取出
    private void removeFromPos(Node target) {
        target.prev.next = target.next;
        target.next.prev = target.prev;
        target.next = null;
        target.prev = null;
    }
}
TSN新技术,让您的设备网络“更实时、更确定、更安全” 近日,工业和信息化部发布了2022年第23号公告,批准发布行业标准YD/T 4134-2022《工业互联网时间敏感网络需求及场景》。该标准是国内首个时间敏感网络(TSN)技术标准,标志着我国TSN技术标准体系建设迈出了坚实的一步,对构建工业互联网网络标准体系具有重要意义。TSN源于Time-Sensitive Networking的缩写,中文译名为时间敏感网络,因其具备的确定性和微秒级交互特性,受到了对实时性要求较高的工业控制领域的关注。TSN与工业数智化。 阅读详情

相关推荐

EduCoder 计算机网络实验 IP协议分析

第1关:跟踪抓包 任务描述 相关知识 IPV4数据包 截获数据报 分析截获结果 使用git将本地文件推送到远程仓库永久保存 操作要求 任务描述 本关任务:使用 Ping 命令发送数据报,用 Wireshark 截获数据报,分析 IP 数据报的格式,理解 IPV4 地址的编址方法,加深对 IP 协议的理解。 相关知识 为了完成本关任务,你需要掌握: 截获数据报; 简单分析数据包格式。 IPV4数据包 网际协议第 4 版(Internet Protocol version 4

qq_41975868的博客 1万+

Leetcode: 146. LRU Cache LRU缓存机制

LRU Cache LRU缓存机制 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。 获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。 写入数据 put(key, value) -如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该...

Coding 336

RocketMQ源码分析之消息轨迹

文章目录前言一、消息轨迹二、消息轨迹源码分析1、producer端2、consumer端三、消息轨迹中存储的信息1.TraceContext2.TraceBean3.TraceType四、总结 前言   本篇文章将会从以下方面介绍消息轨迹:什么是消息轨迹?如何启用消息轨迹?示例demo、消息轨迹的源码分析和消息轨迹中存储的信息。最后会用一张图来总结消息轨迹的工作原理。 一、消息轨迹 1.什么是消息轨迹? 消息轨迹是用来跟踪记录消息发送、消息消费的轨迹。 2.如何启用消息轨迹? broker端 需要在b

不爱学习的小妞博客 1076

LeetCode 146 LRU Cache

本文记录本人在解决 LeetCode 第146题 LRU Cache 过程中的收获。主要是增加了对 C++ STL list 容器的了解。点击该链接可跳转到 LeetCode 此题目处。本文借鉴了花花酱 的个人网站的讲解和代码。本文相当于对花花酱的代码的注释。 需要解决的题目属于模拟题,即模拟出 Least Recently Used Cache 的运行机制。由于题目对 get()、put()函数时间复杂度都做了 常数级的限制。所故题目的难点主要落在用什么数据结构上。在这里直接给出结论,使用一个 list

OrdinaryMatthew的博客 321

你的Wi-Fi总断网?可能是DHCP租约到期惹的祸!手把手教你用Windows/路由器设置永久IP或延长租期

本文深入解析家庭Wi-Fi频繁断网的常见原因——DHCP租约到期问题,并提供Windows和路由器设置永久IP或延长租期的详细教程。通过对比静态IP配置与延长租期两种方案,帮助用户有效解决网络不稳定问题,特别适合游戏玩家和远程工作者优化家庭网络环境。

weixin_29305313的博客 1416

别再傻傻分不清!用R语言5分钟搞定RR、AR、OR的计算与解读(附代码)

本文通过R语言实战演示,详细讲解了RR(相对风险)、AR(绝对风险)和OR(比值比)的计算方法与科学解读。提供完整的代码示例和可视化技巧,帮助医学研究者快速掌握这些核心流行病学指标的应用与常见陷阱规避。

weixin_42526015的博客 264

146. LRU Cache

题目: Design a data structure that follows the constraints of aLeast Recently Used (LRU) cache. Implement theLRUCacheclass: LRUCache(int capacity)Initialize the LRU cache withpositivesizecapacity. int get(int key)Return the value of thekeyif th...

weixin_49991368的博客 148

LRU缓存 Leetocde146. LRU Cache

class Node { public: int key_t; int value_t; Node(int key,int value):key_t(key),value_t(value) { } }; class LRUCache { public: list&lt;Node&gt; cache;//缓存 双向链表 方便插入...

BJUT_bluecat的博客 242

Windows下OpenMBEE项目MMS部署失败经历及改正

在Github里找到了上面的第一份文档,下载了MMS源代码(不知道为什么这个代码跟上面操作克隆得到的MMS代码不一样?好像是因为Gradle的版本问题,我本地之前已经安装过7.6.4的Gradle了,于是我找到gradle-wrapper.properties文件。后面在VScode中运行了一下依然是报这个错误,我在VScode终端直接执行。不知道为什么依然没什么用。打开“Windows PowerShell”。(如果是第一次运行它,将看到更长的输出)导航到要克隆存储库的文件夹,例如,

Yo_lan_da的博客 1868

goahead(嵌入式Web服务器)之文件传输篇

goahead文件上传原理: 使用html form即表单提交文件上传请求,web服务器核心处理接收客户端Post过来的文件数据(注意post的是二进制数据),最后,web服务器把接收到文件数据以二进制格式写到服务器本端存储系统。 goahead文件上传步骤: 1、创建upload.html文件,内容如下: <html> <head> <meta charset="utf-8"/> <title>Upload</title> </head&g

weixin_40732273的博客 3495

LeetCode—146.LRU缓存机制(LRU Cache)——分析及代码(C++)

LeetCode—146.LRU缓存机制[LRU Cache]——分析及代码[C++] 一、题目 二、分析及代码 1. 双向链表+哈希表(优化后) (1)思路 (2)代码 (3)结果 2. 双向链表+哈希表(优化前) (1)思路 (2)代码 (3)结果 三、其他 一、题目 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。 获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数)

江南土豆的博客 723

基于若依的ruoyi-nbcio-plus支持VForm3表单字段数据保存到数据库的一种方法——全网首创(三)

基于若依的ruoyi-nbcio-plus支持VForm3表单字段数据保存到数据库的一种方法——全网首创(三)

宁波阿成的博客 524

【LeetCode 146】 LRU Cache

题目描述 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) of the key i...

iCode_girl的博客 181

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

【leetcode】146. LRU Cache

题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) of the key if...

zxc120389574的博客 149

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

[LeetCode 146] LRU Cache

Design and implement a data structure forLeast Recently Used (LRU) cache. It should support the following operations:getandput. get(key)- Get the value (will always be positive) of the key if th...

ExcitedZhang的博客 113

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Leetcode 146. LRU Cache

原题:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get andput.get(key) - Get the value (will always be positive) of the key if th...

muche001的专栏 188

leetcode 146. LRU Cache

https://leetcode.com/problems/lru-cache/submissions/ Design and implement a data structure forLeast Recently Used (LRU) cache. It should support the following operations:getandput. get(key)- Ge...

MissZhou要努力 215

[LeetCode 解题报告]146. LRU Cache

Design and implement a data structure forLeast Recently Used (LRU) cache. It should support the following operations:getandput. get(key)- Get the value (will always be positive) of the key if th...

CVer 233

leetcode【146】LRU Cache【c++版本,STL应用】

问题描述: Design and implement a data structure forLeast Recently Used (LRU) cache. It should support the following operations:getandput. get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.pu...

fanyuwgy的博客 314

Leetcode: 146. LRU Cache

凌晨1:24,转行中的程序媛,一边敷着面膜一边刷题,画面太美。 既然是在程序人生专栏,应该可以写一点与程序无关的东西。 感恩节长假,放松了四天,今天上班回家累的只想葛优躺,但是还得撑着伺候公主洗漱睡觉,眼睛都睁不开了,好想直接倒头就睡,可是看着临近的面试,不得不撑着。实在受不了,在沙发上睡了一会儿,起来接着刷 LRU Cache 这是个设计题。 Cache一般有几种,分类主要是针对当Cache ...

weixin_43476349的博客 277

LeetCode 146:LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) of the key if the...

qq_34314736的博客 117

LeetCode: 146. LRU Cache

LeetCode: 146. LRU Cache 题目描述 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. get(key) - Get the value (will alwa...

杨领well的专栏 219

模拟卷Leetcode【普通】146. LRU 缓存机制

146. LRU 缓存机制 运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制 。 实现 LRUCache 类: LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存 int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。 void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-

卷子的博客 444

Leetcode进阶之路——LRU Cache C++三种解法

去年网易秋招面试的时候有一道现场手撕LRU的题,跟这道几乎一样,现记录如下: 146. LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. get(key) - Ge...

MaloFleur 3414
上一篇: 139. Word Break (M)
下一篇: 140. Word Break II (H)
绿色小光头
博客等级 码龄10年 10粉丝 301原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值