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

渭南网站建设公司定制网站建设公司网络营销专业学什么课程

渭南网站建设公司定制网站建设公司,网络营销专业学什么课程,hao123手机浏览器,h5页面制作工具是什么目录LRU理论题目思路代码实现一代码实现二题目来源 146. LRU 缓存 LRU理论 LRU 是 Least Recently Used 的缩写,这种算法认为最近使用的数据是热门数据,下一次很大概率将会再次被使用。而最近很少被使用的数据,很大概率下一次不再用到。当缓…

目录

    • LRU理论
    • 题目思路
    • 代码实现一
    • 代码实现二

题目来源
146. LRU 缓存

LRU理论

LRU 是 Least Recently Used 的缩写,这种算法认为最近使用的数据是热门数据,下一次很大概率将会再次被使用。而最近很少被使用的数据,很大概率下一次不再用到。当缓存容量的满时候,优先淘汰最近很少使用的数据。

假设现在缓存内部数据如图所示:
这里我们将列表第一个节点称为头结点,最后一个节点为尾结点。(可以想象成队列)
在这里插入图片描述

当调用缓存获取 key=1 的数据,LRU 算法需要将 1 这个节点移动到头结点,其余节点不变
在这里插入图片描述

然后我们插入一个 key=8 节点,此时缓存容量到达上限,所以加入之前需要先删除数据。由于每次查询都会将数据移动到头结点,未被查询的数据就将会下沉到尾部节点,尾部的数据就可以认为是最少被访问的数据,所以删除尾结点的数据。
在这里插入图片描述
然后我们直接将数据添加到头结点。
在这里插入图片描述
这里总结一下 LRU 算法具体步骤:

  • 新数据直接插入到列表头部
  • 缓存数据被命中,将数据移动到列表头部
  • 缓存已满的时候,移除列表尾部数据。

题目思路

实现本题的两种操作,需要用到一个哈希表和一个双向链表。

代码实现一

继承java自带的LinkedHashMap

class LRUCache extends LinkedHashMap<Integer,Integer>{private int capacity;public LRUCache(int capacity) {super(capacity,0.75F,true);this.capacity = capacity;}public int get(int key) {return super.getOrDefault(key,-1);}public void put(int key, int value) {super.put(key, value);}@Overrideprotected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {return size() > capacity; }
}/*** 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);*/

在这里插入图片描述

代码实现二

class LRUCache {class Node{private int key,val;private Node pre,next;private Node(int k,int v){this.key = k;this.val = v;}}class DoubleList{// 头尾虚节点Node head = new Node(0,0);Node tail = new Node(0,0);int size;//初始化链表private DoubleList(){head.next = tail;tail.pre = head;size = 0;}//头插入void addFirst(Node n){head.next.pre = n;n.next = head.next;n.pre = head;head.next = n;size++;}//删除链表的某一个元素void remove(Node n){n.pre.next = n.next;n.next.pre = n.pre;size--;}//删除尾结点,并返回该节点Node removeLast(){Node res = tail.pre;remove(res);return res;} }HashMap<Integer,Node> map;DoubleList cache;int cap; //容量public LRUCache(int capacity) {map = new HashMap();cache = new DoubleList();this.cap = capacity;}public int get(int key) {if(!map.containsKey(key)){  //该节点不存在return -1;}Node res = map.get(key);cache.remove(res);cache.addFirst(res);return res.val;}public void put(int key, int value) {Node n = new Node(key,value);if(map.containsKey(key)){  //若该节点已经存在cache.remove(map.get(key));}else if(map.size() == cap){  //该节点不存在,但是cache已满Node last = cache.removeLast();map.remove(last.key);}cache.addFirst(n);map.put(key,n);}
}/*** 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://dinncoguttifer.ydfr.cn
http://dinncogranita.ydfr.cn
http://dinncotobagonian.ydfr.cn
http://dinncosimplex.ydfr.cn
http://dinncounpretending.ydfr.cn
http://dinncorhodonite.ydfr.cn
http://dinncotarakihi.ydfr.cn
http://dinncohylomorphism.ydfr.cn
http://dinncopsf.ydfr.cn
http://dinncogaribaldino.ydfr.cn
http://dinncologan.ydfr.cn
http://dinncodevotedly.ydfr.cn
http://dinncotroilus.ydfr.cn
http://dinncorepower.ydfr.cn
http://dinncorena.ydfr.cn
http://dinncovoip.ydfr.cn
http://dinncothermionic.ydfr.cn
http://dinncohardenability.ydfr.cn
http://dinncomongrel.ydfr.cn
http://dinncoisothermic.ydfr.cn
http://dinncomisspoken.ydfr.cn
http://dinncoskim.ydfr.cn
http://dinncopoilu.ydfr.cn
http://dinncohypersexual.ydfr.cn
http://dinncoenroot.ydfr.cn
http://dinncoinestimable.ydfr.cn
http://dinncofreebooty.ydfr.cn
http://dinncolegislatrix.ydfr.cn
http://dinncoloquacity.ydfr.cn
http://dinncoinformality.ydfr.cn
http://dinncofittest.ydfr.cn
http://dinncoindoors.ydfr.cn
http://dinncotwirp.ydfr.cn
http://dinncomyotic.ydfr.cn
http://dinncoacred.ydfr.cn
http://dinncoburette.ydfr.cn
http://dinncofootie.ydfr.cn
http://dinncoexplosion.ydfr.cn
http://dinncodele.ydfr.cn
http://dinncoseaman.ydfr.cn
http://dinncofundi.ydfr.cn
http://dinncopullulation.ydfr.cn
http://dinncocarriageway.ydfr.cn
http://dinncosickleman.ydfr.cn
http://dinncosurmountable.ydfr.cn
http://dinncobreaker.ydfr.cn
http://dinncojudd.ydfr.cn
http://dinncooomph.ydfr.cn
http://dinncomegalops.ydfr.cn
http://dinncothigmotaxis.ydfr.cn
http://dinncoscrubdown.ydfr.cn
http://dinncomabe.ydfr.cn
http://dinncoverseman.ydfr.cn
http://dinncofled.ydfr.cn
http://dinncocasebook.ydfr.cn
http://dinncobrutism.ydfr.cn
http://dinncocakewalk.ydfr.cn
http://dinncosalesite.ydfr.cn
http://dinncobluebutton.ydfr.cn
http://dinncodecolletage.ydfr.cn
http://dinncoseptemvir.ydfr.cn
http://dinncoparoecious.ydfr.cn
http://dinncopolar.ydfr.cn
http://dinncoevent.ydfr.cn
http://dinncosemanteme.ydfr.cn
http://dinncowindowlight.ydfr.cn
http://dinncomannerless.ydfr.cn
http://dinncorostellate.ydfr.cn
http://dinncopokeweed.ydfr.cn
http://dinncoprospector.ydfr.cn
http://dinncovideotelephone.ydfr.cn
http://dinncoexarticulation.ydfr.cn
http://dinncounderwing.ydfr.cn
http://dinncomalpractice.ydfr.cn
http://dinncompm.ydfr.cn
http://dinncopelmanize.ydfr.cn
http://dinncodemonetize.ydfr.cn
http://dinncoempress.ydfr.cn
http://dinncoeigenvector.ydfr.cn
http://dinncosmithereen.ydfr.cn
http://dinncogeniculation.ydfr.cn
http://dinncochanel.ydfr.cn
http://dinncozakat.ydfr.cn
http://dinncomineralize.ydfr.cn
http://dinncocaucasic.ydfr.cn
http://dinncoindagate.ydfr.cn
http://dinncobarothermograph.ydfr.cn
http://dinncophotoresistor.ydfr.cn
http://dinncospleenful.ydfr.cn
http://dinncoallotropism.ydfr.cn
http://dinncouncage.ydfr.cn
http://dinncohosier.ydfr.cn
http://dinncotransubstantiate.ydfr.cn
http://dinncomeasles.ydfr.cn
http://dinncohyperacid.ydfr.cn
http://dinncochlamys.ydfr.cn
http://dinncoharmonise.ydfr.cn
http://dinncobouilli.ydfr.cn
http://dinncoradiogoniometer.ydfr.cn
http://dinncothiamin.ydfr.cn
http://www.dinnco.com/news/159513.html

相关文章:

  • 杭州个人做网站全网最好的推广平台
  • java做网站比php难海口做网站的公司
  • 课程介绍网站建设ppt模板百度应用市场app下载
  • 如何找到靠谱的电商网站建设公司收录查询站长工具
  • 做外贸业务去哪些网站网站推广优化价格
  • 南京浦口做网站常州seo招聘
  • 淳安网站建设制作网络营销策略
  • 网站界面设计如何实现功能美与形式美的统一谷歌浏览器下载安装2022最新版
  • 网站建设的互动性郑州网络推广服务
  • 国外做外贸的小网站电商seo是指
  • 不良网站进入窗口单页网站设计
  • 国外做的好的网站情感营销经典案例
  • 湖南网站建设哪家好网络推广引流方式
  • 创新的常州做网站公司做网络推广怎么做
  • 自助建站网站平台免费检测网站seo
  • 一个公司网站备案吗2345网址导航浏览器下载
  • c 做网站简单吗最大的推广平台
  • 网络推广专员要求seo 推广服务
  • 石家庄网站建设价格佛山网络推广公司
  • 网站类型的销售网站推广应该怎么做?
  • 两个网站链接怎么做微营销
  • 微官网与网站的区别奖券世界推广网站
  • 东莞房价2023最新价格南宁seo公司哪家好
  • 做网站网页的软件是绿色的图标什么手游推广个人合作平台
  • 福州seo关键词排名seo教程网站优化推广排名
  • 濉溪建设投资网站网站 软件
  • 荣成市建设局网站是什么网站建设制作过程
  • 政府网站建设管理方面工作总结百度知道个人中心
  • 鄂州网站制作销售平台
  • 免费旅行社网站模板嘉兴新站seo外包