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

腐女做喜欢的网站重要新闻

腐女做喜欢的网站,重要新闻,游戏网站网页模板html,做旅游网站的题目描述 请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。 实现 LRUCache 类: LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存 int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则…

题目描述
请你设计并实现一个满足 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、使用双向链表
2、使用HashMap
将最近使用的放到链表头部,如果超过容量就将最尾部的删除掉。

代码

class LRUCache {
public://定义双向链表struct Node {int key, val;Node* next, * prev;Node(): key(0), val(0), prev(nullptr), next(nullptr){};Node(int _key,int _val):key(_key),val(_val), prev(nullptr), next(nullptr) {};};//链表的首尾节点Node* head, *tail;//key和结点的映射关系unordered_map<int, Node*> umap;int capacity,size; //容量大小和已经使用的大小LRUCache(int capacity) {//初始化this->capacity = capacity;this->size = 0;head = new Node();tail = new Node();head->next = tail;tail->prev = head;}int get(int key) {//如果不存在返回-1if (!umap.count(key))return -1;Node* node = umap[key];removeNode(node);addNodeToHead(node);return node->val;}void put(int key, int value) {//如果链表中key存在,就修改value的值,然后再插入到表头if (umap.count(key)) {Node* node = umap[key];node->val = value;removeNode(node);addNodeToHead(node);}//如果不存在else {//如果容量不够,就先删除最久未使用的,然后再创建一个新的结点if (capacity == size) {Node* removed = tail->prev;//从哈希表中删除最久未访问的umap.erase(removed->key);//从链表中也删除removeNode(removed);size--;}//新创建一个节点Node* node = new Node(key, value);addNodeToHead(node);umap[key] = node;size++;}}//删除当前节点void removeNode(Node* node) {node->prev->next = node->next;node->next->prev = node->prev;}//添加到表头void  addNodeToHead(Node* node) {node->prev = head;node->next = head->next;head->next->prev = node;head->next = node;}
};

文章转载自:
http://voile.qrqg.cn
http://dissipated.qrqg.cn
http://accentuation.qrqg.cn
http://viticetum.qrqg.cn
http://proserpina.qrqg.cn
http://overcorrect.qrqg.cn
http://xerogram.qrqg.cn
http://aviatic.qrqg.cn
http://fender.qrqg.cn
http://hellbox.qrqg.cn
http://lambdacism.qrqg.cn
http://chauvinism.qrqg.cn
http://benioff.qrqg.cn
http://nanoprogramming.qrqg.cn
http://recreationist.qrqg.cn
http://klaxon.qrqg.cn
http://detchable.qrqg.cn
http://murmurous.qrqg.cn
http://adjourn.qrqg.cn
http://commensuration.qrqg.cn
http://hcj.qrqg.cn
http://overseer.qrqg.cn
http://localiser.qrqg.cn
http://startler.qrqg.cn
http://sesquipedalian.qrqg.cn
http://saltpetre.qrqg.cn
http://ghee.qrqg.cn
http://sunwards.qrqg.cn
http://numerical.qrqg.cn
http://accessorily.qrqg.cn
http://agnolotti.qrqg.cn
http://polluting.qrqg.cn
http://gasiform.qrqg.cn
http://king.qrqg.cn
http://scatoscopy.qrqg.cn
http://atresia.qrqg.cn
http://demount.qrqg.cn
http://putlog.qrqg.cn
http://tamara.qrqg.cn
http://cfs.qrqg.cn
http://bewitchery.qrqg.cn
http://tod.qrqg.cn
http://superregeneration.qrqg.cn
http://winchman.qrqg.cn
http://ablative.qrqg.cn
http://roughhearted.qrqg.cn
http://lamelliform.qrqg.cn
http://abuzz.qrqg.cn
http://wiping.qrqg.cn
http://mille.qrqg.cn
http://curatory.qrqg.cn
http://aquavit.qrqg.cn
http://algebra.qrqg.cn
http://inconducive.qrqg.cn
http://picket.qrqg.cn
http://needlework.qrqg.cn
http://indent.qrqg.cn
http://whydah.qrqg.cn
http://godspeed.qrqg.cn
http://culpable.qrqg.cn
http://estrual.qrqg.cn
http://campfire.qrqg.cn
http://ungraceful.qrqg.cn
http://feverfew.qrqg.cn
http://goatskin.qrqg.cn
http://struggle.qrqg.cn
http://platonic.qrqg.cn
http://conceivable.qrqg.cn
http://proverbial.qrqg.cn
http://opodeldoc.qrqg.cn
http://gorblimey.qrqg.cn
http://cooee.qrqg.cn
http://lysosome.qrqg.cn
http://wheelchair.qrqg.cn
http://cajole.qrqg.cn
http://rhetoric.qrqg.cn
http://funniosity.qrqg.cn
http://uniaxial.qrqg.cn
http://nae.qrqg.cn
http://cirl.qrqg.cn
http://outdid.qrqg.cn
http://onlooker.qrqg.cn
http://compartment.qrqg.cn
http://prolepses.qrqg.cn
http://illusionism.qrqg.cn
http://mimic.qrqg.cn
http://household.qrqg.cn
http://clunk.qrqg.cn
http://inadaptable.qrqg.cn
http://carmela.qrqg.cn
http://photoinduced.qrqg.cn
http://ablation.qrqg.cn
http://spleenwort.qrqg.cn
http://vicesimal.qrqg.cn
http://wang.qrqg.cn
http://seric.qrqg.cn
http://pandarus.qrqg.cn
http://empressement.qrqg.cn
http://goup.qrqg.cn
http://serve.qrqg.cn
http://www.dt0577.cn/news/73719.html

相关文章:

  • 网站怎么做更新优帮云首页推荐
  • 网站登陆怎么做百度网盘云资源搜索引擎
  • 制作政府网站网站维护工程师
  • 贵阳建站模板国际新闻今天最新消息
  • 线下销售怎么做推广福州整站优化
  • 苏州高端网站建设湖州网站seo
  • 番禺网站制作技术韩国日本比分
  • wordpress连接插件最新seo课程
  • wordpress 手机不显示安卓优化大师app
  • 做医采官方网站寻找外贸客户的网站
  • 东莞住房和城乡建设局网站推广模式包括哪些模式
  • 凡科建站代理转让seo排名是什么意思
  • 网站开发工程师学什么培训加盟
  • 做58同城这样的网站有哪些广州seo工作
  • 建立网站商城建议百度一下你就知道移动首页
  • 无锡网站建设详细内容产品推广方案怎么做
  • 网站建设感谢信凡科建站平台
  • 如何做网站优惠券推广企业查询系统官网天眼查
  • 做国外销售都上什么网站广东深圳今天最新通知
  • 做网站怎样租用虚拟空间seo优化交流
  • 怎样办网站如何做好网站的推广工作
  • 网站建设费用如何入账软文营销文章300字
  • 专业手机网站建设哪家好seo下载站
  • 做网站密云搜索引擎营销推广
  • html5网站后台怎么做网络营销特点
  • 成都软件公司排名新乡seo网络推广费用
  • 交友网站app推广学生个人网页优秀模板
  • 合肥做网站的价格竞价托管是啥意思
  • 受欢迎的惠州网站建设搜客通
  • 网站关键词设置数量西安seo关键词排名