LeetCode146. LRU 缓存(2024秋季每日一题 37)

news/2024/10/23 0:58:20/

请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现 LRUCache 类:

  • LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存
  • int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
  • void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。

函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。

示例:

输入

[“LRUCache”, “put”, “put”, “get”, “put”, “get”, “put”, “get”, “get”, “get”]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]

解释

LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1); // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
lRUCache.get(2); // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1); // 返回 -1 (未找到)
lRUCache.get(3); // 返回 3
lRUCache.get(4); // 返回 4

提示:

1 < = c a p a c i t y < = 3000 1 <= capacity <= 3000 1<=capacity<=3000
0 < = k e y < = 10000 0 <= key <= 10000 0<=key<=10000
0 < = v a l u e < = 1 0 5 0 <= value <= 10^5 0<=value<=105
最多调用 2 ∗ 1 0 5 2 * 10^5 2105getput


思路:双链表 + 哈希
时间复杂度:get: O ( 1 ) O(1) O(1)、put: O ( 1 ) O(1) O(1)

  • 初始化缓存时,创建 head,tail 节点,并且相互指向彼此,初始化容器容量、容器节点数量
  • get 时:
    • 如果当前 key 存在,则将当前 key 对应的节点移到链表头部(通过 map 保存),并返回 节点的 val 值
    • 如果当前 key 不存在,则直接返回 -1
  • put 时:
    • 如果当前 key 存在,则通过 hash 表 找到对应的节点,将此节点挪到链表头部,并更新节点的 val 值
    • 如果当前 key 不存在
      • 如果链表中 当前节点数量 < 容量,则新建节点,将节点添加到链表 头部
      • 如果链表中 当前节点数量 >= 容量,则删除尾部节点,新建节点,将节点添加到 链表头部
      • 更新链表中节点 cnt 数量

map:unordered_map<int, Node *> mp;

struct Node{Node * pre, * ne;int key, val;Node(){pre = nullptr;ne = nullptr;key = 0;val = 0;}Node(int _key, int _val){key = _key, val = _val;pre = ne = nullptr;}
};
class LRUCache {
private:Node * head;Node * tail;unordered_map<int, Node *> mp;int cnt;int n;
public:LRUCache(int capacity) {cnt = 0;n = capacity;head = new Node();tail = new Node();head -> ne = tail;tail -> pre = head;}int get(int key) {if(!mp.count(key)){return -1;}Node * cur = mp[key];moveToHead(cur);return cur -> val;}void put(int key, int value) {if(!mp.count(key)){cnt++;Node * cur = new Node(key, value);if(cnt > n){removeTail();addToHead(cur);cnt = n;}else{addToHead(cur);}//mp[key]= cur;mp.insert({key, cur});}else{Node * cur = mp[key];cur -> val = value;moveToHead(cur);}}void removeTail(){mp.erase(tail -> pre -> key);tail -> pre = tail ->pre -> pre;tail -> pre -> ne = tail;}void addToHead(Node * cur){cur -> pre = head;cur -> ne = head -> ne;head -> ne -> pre = cur;head -> ne = cur;}void moveToHead(Node * cur){cur -> ne -> pre = cur -> pre;cur -> pre -> ne = cur -> ne;addToHead(cur);}
};

http://www.ppmy.cn/news/1541198.html

相关文章

mysql多表关系与查询

一、多表关系 1.多表操作分类 1.1 多对一 关系举例&#xff1a; 多对一&#xff1a;多名学生在一个班级里 一对多主要是靠 “外键” 实现。在 “多” 的表中建立外键&#xff0c;指向 "一"的主键 一对多的关系在实际生产当中使用非常常见。 一对多的核心解决方案是…

前端拦截302重定向

背景: 根据业务场景需要拦截302做后续的逻辑处理 尝试一: : axios拦截 、、、、、async created() {// 获取302请求返回的location后手动修改video的src路径let targetSrc;try {await axios.get(this.video).then((res) > {const { headers, status } res;const { locat…

开源医疗管理的未来:参与码良诊所管理系统,助力智能医疗

开源医疗管理的未来&#xff1a;参与码良诊所管理系统&#xff0c;助力智能医疗 引言 在过去的六个多月里&#xff0c;我们公司 码良互联网科技有限公司 专注于开发一个全面、智能的诊所管理系统&#xff0c;旨在帮助中小型医疗机构提升运营效率、优化患者管理流程、以及降低…

CentOS 7 安装gcc编译环境

有时需要使用源码安装某个应用程序&#xff0c;有时还需要对源码进行一定程度的修改和定制才能满足业务需求&#xff0c;有时需要在linux环境下开发某个特定功能的c程序&#xff0c;此时都需要用到gcc编译环境&#xff0c;此时就需要安装gcc编译环境。 在 CentOS 7 上安装 C 编…

Spring Ai 对接智谱清言结合vue(清测成功)

智谱文档&#xff1a;智谱AI开放平台 注意:springboot版本要在3.0以上&#xff0c;pom.xml要配置下载的源。 pml文件如下 建议使用下科学上网~~~ <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.…

linux 查看CPU信息 核心数 逻辑核心数

cat /proc/cpuinfo Linux操作系统的CPU信息被保存在/proc/cpuinfo文件中&#xff0c; processor: 这是逻辑CPU的编号&#xff0c;从0开始。 physical id: 如果你有多个物理CPU&#xff0c;每个物理CPU都会有一个唯一的ID。 core id: 每个核心的唯一ID。有了HT技术后&#xf…

svn安装完成,但在cmd窗口运行是报错svn不是内部或外部命令

已经安装了svn&#xff0c;但是在cmd中输入svn命令的时候提示svn不是内部或外部命令是因为没有安装svn client。 解决办法&#xff1a; windows安装svn的时候默认是不安装 svn comand line这个东西的&#xff0c;重新安装svn客户端&#xff0c;将“command line client tools”…

Lua字符串

软考鸭微信小程序 过软考,来软考鸭! 提供软考免费软考讲解视频、题库、软考试题、软考模考、软考查分、软考咨询等服务 Lua作为一种轻量级、高效的脚本语言&#xff0c;在字符串处理方面提供了丰富的功能和灵活的操作方式。字符串在Lua中是一系列的字节&#xff0c;可以包含任意…