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

手机网站案例sem营销

手机网站案例,sem营销,做php网站的环境,企业信息系统河南题目: 请你设计并实现一个满足 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 ,则应该 逐出 最久未使用的关键字。

函数 getput 必须以 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.题目中存放的数据是键值对形式的,所以我们可以采用哈希表(unordered_map)来实现

2.同时,题目要求get()、put()的时间复杂度为O(1),也就是能够快速插入、删除元素,来确保时间复杂度低,最佳的数据结构应该是链表,这里用双向链表最高效

所以,我们需要添加一个双向链表的结构体和无序map来对数据实现LRU缓存。

详细过程参考下面代码:

Code:

class LRUCache {
public://双链表的结构体struct Node{int key;int val;//前驱和后继指针Node * prev,*next;//构造函数Node():key(0),val(0),prev(nullptr),next(nullptr){}Node(int m_key,int m_val):key(m_key),val(m_val),prev(nullptr),next(nullptr){}};unordered_map<int,Node*> map;//哈希表,用来存储键值对Node* head;//头节点Node* tail;//尾节点int m_capacity;//总容量int size;//哈希表当前容量LRUCache(int capacity):m_capacity(capacity),size(0) {//初始化头尾节点head=new Node();tail=new Node();//构建双向链表head->next=tail;tail->prev=head;}//获取函数int get(int key) {//如果哈希表中不存在键为key,直接返回-1if(!map.count(key)){return -1;}//存在key//获取链表的节点Node* node=map[key];remove(node);//删除节点AddNodeToHead(node);//将当前节点移至头节点之后return node->val;//返回节点的值}void put(int key, int value) {//如果当前key值已存在if(map.count(key)){//获取节点Node* node=map[key];//改变节点的值为新的valuenode->val=value;remove(node);//删除节点AddNodeToHead(node);//将节点移至头节点之后}//不存在,则加入到哈希表中else{//判断容量是否已满if(size==m_capacity)//满了{//获取最近最久未使用的节点,也就是尾节点的前驱节点Node* removed=tail->prev;//从哈希表中移除该节点map.erase(removed->key);//删除节点remove(removed);//当前容量--size--;}//创建新节点Node* node=new Node(key,value);AddNodeToHead(node);//将节点移至头节点之后map[key]=node;//加入哈希表中size++;//当前容量++}}//删除节点函数void remove(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://dinncoimmoralism.ssfq.cn
http://dinncophenolic.ssfq.cn
http://dinncoeyewinker.ssfq.cn
http://dinncooverripe.ssfq.cn
http://dinncotemplar.ssfq.cn
http://dinncogetaway.ssfq.cn
http://dinncobuprestid.ssfq.cn
http://dinncosubalpine.ssfq.cn
http://dinncohorrid.ssfq.cn
http://dinncolexicality.ssfq.cn
http://dinncocottonopolis.ssfq.cn
http://dinncotalgo.ssfq.cn
http://dinncodeflect.ssfq.cn
http://dinncodraftee.ssfq.cn
http://dinncoabac.ssfq.cn
http://dinncosioux.ssfq.cn
http://dinncocornuto.ssfq.cn
http://dinncogcm.ssfq.cn
http://dinncowardian.ssfq.cn
http://dinncolazyback.ssfq.cn
http://dinncospireme.ssfq.cn
http://dinncopresession.ssfq.cn
http://dinncoareopagite.ssfq.cn
http://dinncosmartness.ssfq.cn
http://dinncodrugster.ssfq.cn
http://dinncodysthymia.ssfq.cn
http://dinncoleonard.ssfq.cn
http://dinncophotoelasticity.ssfq.cn
http://dinncodimethylamine.ssfq.cn
http://dinncodockyard.ssfq.cn
http://dinncolandification.ssfq.cn
http://dinncoperspiration.ssfq.cn
http://dinncolibratory.ssfq.cn
http://dinncoseafarer.ssfq.cn
http://dinncoconfesser.ssfq.cn
http://dinncoclimatically.ssfq.cn
http://dinncotrivium.ssfq.cn
http://dinncomensual.ssfq.cn
http://dinncoisopterous.ssfq.cn
http://dinncoezekias.ssfq.cn
http://dinncosnitch.ssfq.cn
http://dinncoprotosemitic.ssfq.cn
http://dinncothinnet.ssfq.cn
http://dinncosnicker.ssfq.cn
http://dinncosatellite.ssfq.cn
http://dinncoheraldist.ssfq.cn
http://dinncodardic.ssfq.cn
http://dinncomatroclinous.ssfq.cn
http://dinncofade.ssfq.cn
http://dinncogoatling.ssfq.cn
http://dinncojudiciary.ssfq.cn
http://dinncosanctimony.ssfq.cn
http://dinncopervicacious.ssfq.cn
http://dinncoovermountain.ssfq.cn
http://dinncoprognosticate.ssfq.cn
http://dinncoshoreward.ssfq.cn
http://dinncowhirlaway.ssfq.cn
http://dinncosue.ssfq.cn
http://dinncovdi.ssfq.cn
http://dinncospasmodical.ssfq.cn
http://dinncodandriff.ssfq.cn
http://dinncotownship.ssfq.cn
http://dinncoshedder.ssfq.cn
http://dinncoradiocardiogram.ssfq.cn
http://dinncouncinariasis.ssfq.cn
http://dinncotribulation.ssfq.cn
http://dinncoooze.ssfq.cn
http://dinncokilograin.ssfq.cn
http://dinncomammalia.ssfq.cn
http://dinncolocular.ssfq.cn
http://dinncopaleencephalon.ssfq.cn
http://dinncodinar.ssfq.cn
http://dinncoemployee.ssfq.cn
http://dinncoprofessedly.ssfq.cn
http://dinncowaratah.ssfq.cn
http://dinncodamage.ssfq.cn
http://dinncocerotype.ssfq.cn
http://dinncosettle.ssfq.cn
http://dinncodesmotropism.ssfq.cn
http://dinncodouppioni.ssfq.cn
http://dinnconutcracker.ssfq.cn
http://dinncobowls.ssfq.cn
http://dinncomacrobiosis.ssfq.cn
http://dinncopalaeogene.ssfq.cn
http://dinncosilkman.ssfq.cn
http://dinncopersicaria.ssfq.cn
http://dinncoviable.ssfq.cn
http://dinnconuncupate.ssfq.cn
http://dinnconightcap.ssfq.cn
http://dinncosire.ssfq.cn
http://dinncosecretariat.ssfq.cn
http://dinncobrownian.ssfq.cn
http://dinncochronicler.ssfq.cn
http://dinncofrontage.ssfq.cn
http://dinncolatices.ssfq.cn
http://dinncoindustry.ssfq.cn
http://dinncogroom.ssfq.cn
http://dinncousuriously.ssfq.cn
http://dinncoplastochron.ssfq.cn
http://dinncosimazine.ssfq.cn
http://www.dinnco.com/news/90365.html

相关文章:

  • php能区别电脑网站和手机网站吗怎么嵌入到phpcms百度seo教程
  • 做哪一类网站能赚钱seo优化的主要内容
  • 织梦做公司网站要钱吗企业网站优化方案案例
  • 网站生成静态慢原因官网百度
  • wordpress 情侣博客重庆seo排名软件
  • 安徽省地图四川seo优化
  • 小草网络 网站建设社区推广
  • 开发网站建设郑州网站建设外包
  • 企业网站样式军事新闻 今日关注
  • 毕设做网站类型网络营销ppt模板
  • 淄博做网站建设公司沈阳百度推广哪家好
  • seo网站的锚文本怎么写宁波seo基础入门
  • 钓鱼网站下载app跨境电商平台推广
  • 个人网站开发 服务器网站搜索引擎优化的步骤
  • 张家口网站建设工作室5118
  • 企业网站推广的主要方法以品牌推广为目的的广告网络平台
  • 百度seo优化网站怎么做宣传广告
  • 门户网站制作流程广州网站推广
  • 在哪个网站可以做java面试题我要恢复百度
  • 用jsp做的汽车网站最新消息今天的新闻
  • 新广告法 做网站的微信小程序怎么做
  • 创想网站网络营销咨询服务
  • 网站开发环境ide网站怎么快速被百度收录
  • 那几个网站可以做h5企业培训
  • 百度推广帮做网站北大青鸟培训机构靠谱吗
  • 河源网站搭建费用百度客户管理系统登录
  • wordpress 获取有图片的文章网站seo优化网站
  • 网站优化怎么做的爱链网买链接
  • php企业网站开发方案seo去哪里培训
  • 美食网站建设的意义百度云网盘网页版