【Leetcode】 LRU Cache

图解LeetCode:LRU cache 这是LeetCode中第146号题目,不多说,这也是一个准备面试的同学必备的一道题目,出镜率非常高。那么什么是LRU cache呢,假设有一个大小为6的数组,最开始保存的是[1,2,3,... 阅读详情

题目链接:https://leetcode.com/problems/lru-cache/
题目:

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

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(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.

思路:

用链表存储key的顺序关系,不管是用栈还是队列,在get元素和set已有元素时候,需要remove链表中的元素,如果用Java内置的链表,时间复杂度为O(n)。   会超时。

如果我们用HashMap保存每个key对应的链表结点的指针,那么remove的时候就可以根据key获取相应的指针,时间复杂度为O(1)。因为涉及到链表的删除,我们以前总结过,有头结点可以简化判断,又因为删除需要知道被删除结点的前后指针,所以用双向链表。又为了简化对尾结点删除的判断,加一个尾结点。

算法:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class Node {  
  2.     Node pre, next;  
  3.     int key, value;  
  4.   
  5.     Node(int key, int value, Node pre, Node next) {  
  6.         this.key = key;  
  7.         this.value = value;  
  8.         this.pre = pre;  
  9.         this.next = next;  
  10.     }  
  11. }  
  12.   
  13. HashMap<Integer, Node> maps = new HashMap<Integer, Node>();  
  14. int capacity = 0;  
  15. Node head = null;  
  16. Node rear = null;  
  17.   
  18. public LRUCache(int capacity) {  
  19.     this.capacity = capacity;  
  20.     head = new Node(0, 0, null, null);  
  21.     rear = new Node(0, 0, null, null);  
  22.     head.next = rear;  
  23.     rear.pre = head;  
  24. }  
  25.   
  26. public int get(int key) {  
  27.     if (maps.containsKey(key)) {  
  28.         Node node = maps.get(key);  
  29.         // remove node  
  30.         node.pre.next = node.next;  
  31.         node.next.pre = node.pre;  
  32.         // put head  
  33.         node.next = head.next;  
  34.         head.next.pre = node;  
  35.         head.next = node;  
  36.         node.pre = head;  
  37.         return node.value;  
  38.     } else {  
  39.         return -1;  
  40.     }  
  41. }  
  42.   
  43. public void set(int key, int value) {  
  44.     if (maps.containsKey(key)) {  
  45.         Node delete = maps.get(key);  
  46.         // remove node  
  47.         delete.pre.next = delete.next;  
  48.         delete.next.pre = delete.pre;  
  49.     } else if (maps.size() == capacity) {  
  50.         maps.remove(rear.pre.key);  
  51.         // remove the last element  
  52.         rear.pre.pre.next = rear;  
  53.         rear.pre = rear.pre.pre;  
  54.         rear.next = null;  
  55.     }  
  56.     Node node = new Node(key, value, null, null);  
  57.     // put head element  
  58.     node.next = head.next;  
  59.     head.next.pre = node;  
  60.     head.next = node;  
  61.     node.pre = head;  
  62.     maps.put(key, node);  
  63. }  

图解LeetCode146:LRU cache 文章目录思考过程代码实现总结 这是LeetCode中第146号题目,不多说,这也是一个准备面试的同学必备的一道题目,出镜率非常高。 那么什么是LRU cache呢,假设有一个大小为6的数组,最开始保存的是[1,2,3,4,5,6],如果这个数组就是LRU cache的话会有这样的性质: 一图胜千言,现在你应该明白什么是LRU cache了吧。 LRU的全称是Least Recently Used... 阅读详情

相关推荐

RoboMaster新手必看:用STM32 HAL库搞定大疆M3508电机正反转(附完整代码)

本文详细介绍了如何使用STM32 HAL库驱动大疆M3508电机实现正反转控制,包括硬件准备、PWM配置、电机校准及完整代码实现。特别针对RoboMaster新手常见问题提供了解决方案,如电机抖动、不转等故障排查,并分享了缓启动、DMA更新等进阶技巧,帮助开发者快速掌握电机控制核心技术。

weixin_42519985的博客 234

leetcode146、OrderedDict与lru_cache

本文探讨了Python中实现LRU缓存的两种方法:使用OrderedDict和哈希表+双向链表。首先介绍了leetcode146题LRU缓存的OrderedDict解法,该结构通过move_to_end和popitem方法自然契合LRU思想。然后分析了Python字典(dict)在3.6版本后实现的有序性特性,并指出其与OrderedDict的区别。最后深入研究了标准库functools.lru_cache的实现细节,展示了其采用哈希表+双向链表的方案,包括缓存命中和未命中时的处理逻辑、线程安全机制等核心实

usiel的博客 1025

麒麟银河操作系统V10部署ffmpeg(也能用于Linux系统)

部署ffmpeg用来处理视频的各种操作,如编码解码,格式转换,剪辑,处理实时视频流,音频流,加滤镜等

zhouwenxing666的博客 9055

leetCode LRU Cache

点击打开链接

hechenghai 642

feko金属球远场RCS双站

feko金属球远场RCS双站

a264672的博客 6920

MLX90640 热成像 STM32

MLX90640 热成像

zhaocundang's blog 5015

Leetcode 146. LRU Cache

Leetcode 146. LRU Cache题目:解法1:ordered dictionary解法2:Hashmap+double linked list 题目: 解法1:ordered dictionary 用python内置的工具,关于这种解法不做详述,因为LRU这个题目肯定还是要要考察他的具体实现的,所以这种解法参考Leetcode官方即可: https://leetcode.com/problems/lru-cache/solution/ 解法2:Hashmap+double linked li

qq_37821701的博客 358

手把手教你用DCA1000EVM解析IWR6843毫米波雷达数据(附MATLAB代码)

本文提供了一份详尽的实战指南,手把手教你使用DCA1000EVM采集并解析TI毫米波雷达IWR6843的原始数据。内容涵盖硬件连接配置、mmWave Studio参数设置、LVDS数据流结构解析,并附有核心MATLAB代码,详细演示了从原始二进制文件到距离-速度谱的信号处理全链路,帮助开发者快速掌握数据解析与处理的关键步骤。

terraform7cloud的博客 233

LeetCode[One hard A Day] - LRU Cache

LRU Cache题目需求:1)存入key and value, 可以通过key获得value                        可以用hashmap来实现(unordered_map)               2)当capacity不够时,remove掉least recently used                        可以用double linked-list...

real_lisa的博客 175

drm 之modes

#define _GNU_SOURCE #include <errno.h> #include <fcntl.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/mman.h> #include <time.h&...

u012794472的专栏 3726

YOLO26涨点改进| TGRS 2026 | 独家自研创新、检测头改进篇| AHATHead自适应混合注意力检测头,二次创新,强化特征表达与目标判别能力,适合遥感小目标检测、小目标检测有效涨点

🔥本文提出了一种改进YOLO26模型的AHATHead自适应混合注意力检测头,通过融合全局与局部注意力机制增强特征表达能力。该方法采用动态权重分配策略,在预测阶段自动调整关键区域与通道特征权重,有效提升小目标和密集场景的检测性能。实验表明,AHATHead在保持较高推理效率的同时,显著提高了目标定位精度与类别判别能力。文章详细介绍了模块结构、实现原理和代码集成步骤,并提供了两种改进配置方案(yolo26_AHATHead.yaml和yolo26_RFHA-YOLO-AHATHead.yaml)

Ai缝合怪、助力小伙伴高效发论文 171

【Leetcode】146. LRU Cache

题目地址: https://leetcode.com/problems/lru-cache/ Least Recently Used Cache。Java里有LinkedHashMap可以直接用。代码如下: import java.util.LinkedHashMap; public class LRUCache extends LinkedHashMap<Integer, Integer...

数学、算法爱好者的博客 530

YOLOv8改进,YOLOv8添加DCNv3可变性卷积,无需编译

DCNv3在DCNv2版本上进行了改进,上一期我们在YOLOv8中添加了DCNv2,这一期我们将添加DCNv3,DCNv3代码详解可以参考链接:代码详解——可变形卷积(DCNv3)

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 5027

[leetcode] 146. LRU Cache

感想 这道题在leetcode是hard的难度,通过率只有21.2%,我记得在某次笔试的时候做到了这一道题,那时候做得不怎么好,我这里把记录一下我以前写的代码,就当复习一下 题目 Design and implement a data structure forLeast Recently Used (LRU) cache. It should support the following ...

Learning from the mistakes 464

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

【LeetCode】LRU Cache

LeetCode - LRU Cache 复杂度为O(1)的C++解决方案

Velconia的专栏 1069

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Leetcode: LRU Cache

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

ZkvIA的博客 1万+

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Leetcode之LRU Cache

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

TO TOP CODER 909

LeetCode 146. LRU 缓存机制(LRU Cache)

易错点正确做法说明更新数据未移动位置保证“最近使用”put 超过容量未淘汰使用删除头部get 不存在时返回错误返回-1使用是 Python 实现 LRU 的最简洁且效率最高的方式。同时掌握哈希表 + 双向链表手动实现 LRU 也是面试时加分项。本题考查的是缓存机制 + 数据结构的组合使用,属于高频面试题之一。错误点正确方式删除时未断开前后节点_remove()正确处理 prev/next忘记从cache删除淘汰节点双向链表头尾更新顺序错误始终更新 prev/next 的双向性。

2501_91641025的博客 1071

leetcode LRU Cache

题目大意就是实现一个使用最近最少shiyon

必然中的偶然 751

LeetCode---146. LRU Cache

LeetCode—146. LRU Cache ####题目

pnnngchg的博客 272

力扣经典题解:LRU缓存机制(146. LRU Cache)数据结构设计艺术

LRU 缓存设计完美展现了 ​数据结构组合​哈希表​:提供闪电般的键值查找能力​双向链表​:优雅维护访问时间序列​虚节点​:巧妙处理边界情况这种设计模式在系统开发中广泛应用,掌握它不仅提升算法能力,更能加深对实际系统设计的理解。算法如艺术,好的数据结构组合如同优美的建筑结构。LRU缓存设计的精妙之处在于其简单的结构达成了高效的操作,这正是计算机科学的魅力所在。

sheep404的博客 822

[LeetCode] LRU Cache, Solution

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

codingtmd_in_china的博客 359

[力扣146. LRU 缓存 ](https://leetcode.cn/problems/lru-cache/description/)

LRU是Lease Recently User的缩写,意思是最近 最少使用。比如设计一个文件缓存系统,每个文件有自己的大小和访问时间,文件缓存系统有总的大小,当往这个文件系统中放入新的文件时,如果发现超出文件缓存系统的容量,那么把访问时间最旧的文件删掉。使用LinkedHashmap(HashMap的子类,能够记住插入数据的顺序).

m0_37938791的博客 1363

【LeetCode刷题记录】LRU Cache

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

guokaiwhu 689

LeetCode LRU Cache

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

FightForProgrammer的专栏 826
上一篇: 【Leetcode】Min Stack
下一篇: 【Leetcode】Unique Binary Search Trees
wuezs
博客等级 码龄15年 76粉丝 305原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值