HOT35-LRU缓存

news/2024/7/5 5:42:15

    leetcode原题链接:LRU缓存

题目描述

       请你设计并实现一个满足  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

解题方法:用list + map实现。list保存<key, value> pair, map保存<key, list::iter>。put和get分别要注意以下细节问题:

put注意:

1.当前put的<key,value>中的key如果已经在map中,则只需要将list中的<key, value>挪到list的首部,同时更新map中该key对应的value。

2. 如果put的<key,value>中的key不在map中,则需要考虑:

     (1)list是否达到了最大容量,如果没有达到最大容量,需要先删除list的最后一个元素;

     (2)从list的首部插入<key,value>,同时将<key, list.begin()>更新到map中。

 get注意:

1. 如果待查询的key不在map中,则直接返回-1;

2. 如果待查询的key在map中,则:

    (1)将该key对应的list中的节点从当前查询的位置挪到list的头部。

    (2) 更新map中key对应的值为list的begin()。

      (3) 返回key对应的value。

3. 对应c++,这里会频繁的用到三个list的四个函数,分别是:

      list::back() -->获取list的最后一个元素的值

      list::push_front() -->从list的头部插入元素

      list::pop_back() -->从list的尾部删除元素

      list::splice(iter1, list2, iter2)-->将list2的iter2的元素挪到list的iter1的位置,如:lst.splice(list.begin(), lit, iter)表示将lst的iter对应的元素移到list的头部。

C++代码

#include <map>
#include <list>
class LRUCache {
public:
    LRUCache(int capacity) {
        m_capacity = capacity;
    }
    int get(int key);//查询
    void put(int key, int value);//插入
private:
    int m_capacity;
    using NodeType = std::pair<int, int>;
    using ListIter = std::list<NodeType>::iterator;
    using MapIter = std::map<int, ListIter>::iterator;
    std::list<NodeType> m_list;//利用list实现o(1)删除和插入
    std::map<int, ListIter> m_mp;//利用map实现o(1)查询
};
int LRUCache::get(int key) {
    if (!m_mp.count(key)) { //元素不存在
        return -1;
    }
    ListIter list_iter = m_mp[key];
    m_list.splice(m_list.begin(), m_list, list_iter);//将结点移动到链表的头部
    m_mp[key] = m_list.begin();//更新map中key对应的value
    return m_mp[key]->second;
}

void LRUCache::put(int key, int value) {
    if (m_mp.count(key)) { //待插入的元素存在
        // 1. 更新key对应的value值
        ListIter p_node = m_mp[key];
        if (p_node != m_list.end()) {
            p_node->second = value;
        }
        // 2. 将<key, value>移到list的首位
        m_list.splice(m_list.begin(), m_list, m_mp[key]);
        // 3. 更新mp中key对应的位置
        m_mp[key] = m_list.begin();
        return;
    } 
    // 待插入的key不存在
    int n = m_list.size();
    if (n == m_capacity) { //当前元素个数满了
        // 1.先将list尾部的元素汰换
        m_mp.erase(m_list.back().first);
        m_list.pop_back();
        
    }
    // 2.在list头部插入新元素
    m_list.push_front({key, value});
    // 3.更新map的位置
    m_mp[key] = m_list.begin();
}
/**
 * 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);
 */


http://lihuaxi.xjx100.cn/news/1305220.html

相关文章

【算法】Prime Pairs With Target Sum 和等于目标值的质数对

文章目录 Prime Pairs With Target Sum 和等于目标值的质数对问题描述&#xff1a;分析代码 Tag Prime Pairs With Target Sum 和等于目标值的质数对 问题描述&#xff1a; 给你一个整数 n 。如果两个整数 x 和 y 满足下述条件&#xff0c;则认为二者形成一个质数对&#xff…

cf1406 C 树的重心

题意&#xff1a;https://www.luogu.com.cn/problem/CF1406C 思路&#xff1a;首先需要知道树的重心的一些性质&#xff0c;可以看看这篇文章 https://zhuanlan.zhihu.com/p/357938161 那么这题就是找出重心&#xff0c;如果有两个就将他的一个子树移到另一个重心上。 /*ke…

Elasticsearch实战(二十四)---ES数据建模一对多模型Nested结构

Elasticsearch实战—ES数据建模一对多模型Nested结构 文章目录 Elasticsearch实战---ES数据建模一对多模型Nested结构1.ES 一对多模型Nested 结构模型实战2.ES字段查询2.1 非Nested 错误结构及错误查询2.2 Nested结构&#xff0c;正确查询 3.Nested结构原理 我们如何把Mysql的模…

初步学习使用SpringBoot框架

对于SpringBoot框架介绍大家可以看看这个这篇文章&#xff0c;SpringBoot优缺点以及如何安装使用 以下我是按照老师给的安装方法进行安装使用SpringBoot框架&#xff1a; 大家安装SpringBoot框架时候&#xff0c;最好安装3.0以下的&#xff0c;不然需要对应较高版本的JDK版本&…

快速排序的三路划分方法和归并排序的递归和非递归实现

目录 快速排序的三路划分方法 归并排序的递归实现 归并排序的非递归实现 快速排序的三路划分方法 首先快排的时间复杂度为O(N*logN)&#xff0c;空间复杂度O(logN),不稳定。 三路划分&#xff1a;将数据分为三份&#xff1b;可以提高当数据中出现多个重复数字时的效率。 …

有没有免费提取音频的软件,分享几个给大家!

在日常生活中&#xff0c;我们经常遇到需要从视频中提取音频的情况&#xff0c;无论是为了制作音频片段、录制语音笔记还是进行后期编辑。本文将介绍三种免费提取音频的方法&#xff0c;分别是记灵在线工具、PR&#xff08;Adobe Premiere Pro&#xff09;和剪映。通过这些方法…

【新版系统架构】第九章-软件可靠性基础知识

软考-系统架构设计师知识点提炼-系统架构设计师教程&#xff08;第2版&#xff09; 第一章-绪论第二章-计算机系统基础知识&#xff08;一&#xff09;第二章-计算机系统基础知识&#xff08;二&#xff09;第三章-信息系统基础知识第四章-信息安全技术基础知识第五章-软件工程…

基于Springboot+mybatis+mysql+vue实现企业注册模块功能

基于Springbootmybatismysqlvue实现企业注册模块功能 一、系统介绍二、功能展示1.主页面2.注册成功 三、数据库四、代码展示四、其他系统实现五、获取源码 一、系统介绍 该系统实现简单的企业信息注册&#xff0c;保存后&#xff0c;提示注册成功。 运行环境&#xff1a;idea…