当前位置: 首页 > news >正文

LRU Cache 最近最少使用

146. LRU 缓存 - 力扣(LeetCode)

链表:增删改O(1)
哈希表:增删改查O(1)

模板:

class LRUCache 
{
public:LRUCache(int capacity) :_capacity(capacity){}int get(int key) {auto iter = _hashmap.find(key);if(iter != _hashmap.end()){auto listIter = iter->second;pair<int,int> tmp = *listIter;_list.erase(listIter);_list.push_front(tmp);_hashmap[key] = _list.begin();return tmp.second;}return -1;}void put(int key, int value) {auto iter = _hashmap.find(key);//不存在if(iter == _hashmap.end()){//LRU淘汰if(_capacity <= _list.size()){_hashmap.erase(_list.back().first);_list.pop_back();}_list.push_front(make_pair(key,value));_hashmap[key] = _list.begin();}else{auto listIter = iter->second;pair<int,int> tmp = *listIter;tmp.second = value;_list.erase(listIter);_list.push_front(tmp);_hashmap[key] = _list.begin();}}
private:size_t _capacity;list<pair<int,int>> _list; //使用list--增删改O(1)unordered_map<int, list<pair<int,int>>::iterator> _hashmap; //使用unordered_map--增删查改O(1)
};

http://www.dtcms.com/a/487592.html

相关文章:

  • h5游戏免费下载:跳一跳
  • 用asp做网站深圳市点击未来科技网站建设
  • EtherCAT转EtherNet/IP工业模块:数据网关实现电子制造业协议转换
  • 傻瓜式建网站泰兴市建设局网站
  • 制作一个网站做网站做哪个
  • SkipList跳表
  • BEVFUSION解读(八)
  • 网站的icp备案怎么在百度做网站
  • SAP MM 自制=>外协加工
  • Spring Boot临时解决循环依赖注入问题
  • 数字身份安全在重点行业的应用与机遇:现状复盘、需求洞察与趋势展望
  • 网站三元素怎么做网站流量建设
  • wordpress单本小说站wordpress如何做页面模板下载
  • seo在网站建设中的作用网站开发怎么使用维语
  • 个人创业:如何找到适合自己且值得投入的小项目?
  • Go 语言开发工具
  • Golang并发编程篇001_并发编程相关概念解释
  • 营销型网站建设易网拓网站要求wordpress
  • QIIME 2 2025.7:微生物数据分析再升级!
  • 高斯分布的贝叶斯参数估计(Bayesian estimation)
  • 招聘网站建设销售wordpress 注册链接
  • 公众号兼职网站开发设计素材网站花瓣
  • 操作系统3.2 虚拟内存管理
  • 惠州市做网站的公司博客类网站怎么做
  • SpringBoot民宿管理系统l2548(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 手机网站建设哪里好wordpress 访问地址修改密码
  • 昆明汽车建站网站模板视频剪辑培训比较有名的学校
  • ollama记录
  • 【数据结构01课_绪论】
  • chage -d 0 强制密码修改的完整流程