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

红旗网站建设提升seo排名的方法

红旗网站建设,提升seo排名的方法,wordpress 外部链接插件,大连网站建设仟亿科技目录 1.什么是LFU缓存? 2.LFU的使用场景有哪些? 3.LFU缓存的实现方式有哪些? 4.put/get 函数实现具体功能 1.什么是LFU缓存? LFU缓存是一个具有指定大小的缓存,随着添加元素的增加,达到容量的上限&…

目录

1.什么是LFU缓存?

2.LFU的使用场景有哪些?

3.LFU缓存的实现方式有哪些?

4.put/get 函数实现具体功能


1.什么是LFU缓存?

        LFU缓存是一个具有指定大小的缓存,随着添加元素的增加,达到容量的上限,会最先移除最少使用频率的值。如果最少使用频率的值有多个,按照插入的先后顺序移除要求put/get操作的时间复杂度O(1)

2.LFU的使用场景有哪些?

       redis 缓存的过期淘汰策略,leetcode 460. LFU 缓存设计

3.LFU缓存的实现方式有哪些?

      LFU实现的底层数据结构图如下

     有两种实现方式,核心都是两个hashmash

     两个hashmap的含义是

      cache是 map(key,node), key是正常的key值,value是node节点。

      freqMap是map(key,nodelist), key是频率值,value是相同频率的key组成的双向链表。插入使用头插法,尾结点是最早未使用的节点,删除节点时删除尾结点。

      实现思路:

      要求get/put操作时间复杂度是O(1),cache(map)保证get/put操作的时间复杂度是O(1),freqMap中双向链表保证在头部和尾部添加元素的时间复杂度是O(1),自实现的双向链表保证删除任何元素的时间复杂度是O(1)

4.put/get 函数实现具体功能

put(key,value)函数功能实现

cache Map(key,node) 其中key是添加元素的key
freqMap map(key,nodelist)其中 key是频率
分三种情况
1.key不在在cache中不存在,没有达到容量上限- freq++- nodelist新增node,freqMap新增(key,nodelist),cacheMap 新增(key,node),size++
2.key在cache中不存在,达到容量上限- 获得最小频率的nodelist中的第一个元素,freqMap中删除的listnode的node,cache中也删除对应的key- freq++- nodelist新增node,freqMap新增(key,nodelist)和cacheMap新增(key,node)- size不变(因为删除了一个元素,添加了一个元素)
3.key在cache中
步骤如下:-  更新value值-  freqMap中删除的listnode的node,如果当前频率是最小值且list列表为空,min++-  freq++-  nodelist新增node,freqMap新增(key,nodelist),cache新增(key,node)

get(key)函数功能实现

分两种情况
1.key不在缓存中,直接返回-1.
2.key在缓存中- freqMap 删除旧key中nodelist对应的node节点(如果node的频率等于最小频率,且删除后的nodelist为空,更新最小频率的标记min为min+1),- freq++- freqMap中新key对应的nodelist新增node节点,更新cache中的(key,node)信息

两个hashmap,其中nodelist使用jdk自带的linkedhashset的代码实现如下

package com.mashibing.my;import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;public class LFUCache3 {class Node {int key;int value;int freq;public Node(int key, int value) {this.key = key;this.value = value;}}Map<Integer, Node> cache = new HashMap<>();Map<Integer, LinkedHashSet<Node>> freqMap = new HashMap<>();int size;int capacity;int min;public static void main(String[] args) {LFUCache3 cache = new LFUCache3(2);cache.put(1, 1);cache.put(2, 2);// 返回 1System.out.println(cache.get(1));cache.put(3, 3);    // 去除 key 2// 返回 -1 (未找到key 2)System.out.println(cache.get(2));// 返回 3System.out.println(cache.get(3));cache.put(4, 4);    // 去除 key 1// 返回 -1 (未找到 key 1)System.out.println(cache.get(1));// 返回 3System.out.println(cache.get(3));// 返回 4System.out.println(cache.get(4));}public LFUCache3(int capacity) {this.capacity = capacity;}public int get(int key) {Node node = cache.get(key);if (node == null) {return -1;}freqInc(node);return node.value;}//put分三种情况//1.key在cache中不存在,没有达到容量上限,freq值增加1,freqMap,cacheMap 新增(key,value),size++//2.key在cache中不存在,达到容量上限,获得最小频率的set,删除第一个元素,cache中也删除,//同时freqMap和cacheMap(key,value),size不变,因为删除了一个元素,添加了一个元素//3.key在cache中//步骤如下//3.1 更新value值//3.2 删除freqMap从对应key的set集合中删除元素,如果当前频率是最小值且list列表为空,min++//3.3. freq++//3.4 freqMap新增(key,value),更新cache中的valuepublic void put(int key, int value) {Node node = cache.get(key);if (node == null) {Node n = new Node(key, value);if (size == capacity) {//移除最小容量的元素//移除map中的keyLinkedHashSet<Node> set1 = freqMap.get(min);if (set1 != null) {//获得列表的第一个元素Node o = set1.iterator().next();set1.remove(o);cache.remove(o.key);}n.freq++;AddNode(n);} else {n.freq++;AddNode(n);size++;min = 1;}} else {node.value = value;//先从旧的set中删除nodefreqInc(node);}}public void freqInc(Node node) {LinkedHashSet<Node> set = freqMap.get(node.freq);//时间复杂度O(n) 需要自实现频次链表set.remove(node);if (node.freq == min && set.size() == 0) {min++;}node.freq++;AddNode(node);}public void AddNode(Node node) {LinkedHashSet<Node> set = freqMap.get(node.freq);if (set == null) {set = new LinkedHashSet<>();}set.add(node);freqMap.put(node.key, set);cache.put(node.key, node);}
}

两个hashmap,nodelist使用自实现的linkedlist的代码实现如下

package com.mashibing.my;import java.util.HashMap;
import java.util.Map;//双map+自实现linkedlist
public class LFUCache4 {class Node {int key;int value;int freq;Node pre;Node next;public Node(int key, int value) {this.key = key;this.value = value;}public Node() {}}class DoubleLinkedList<N> {Node head, tail;//初始化双向循环链表public DoubleLinkedList() {head = new Node();tail = new Node();head.next = tail;tail.pre = head;}//头插法,新加入的节点放在节点的头部,最久未访问的节点放在尾部public void addNode(Node n) {Node next = head.next;n.next = next;n.pre = head;head.next = n;next.pre = n;}public void deleteNode(Node n) {n.pre.next = n.next;n.next.pre = n.pre;}public boolean isEmpty() {return head.next == tail;}}//cache的key是默认的keyMap<Integer, Node> cache = new HashMap<>();//freq的key是词频,相同词频的node链成一个双向链表Map<Integer, DoubleLinkedList<Node>> freqMap = new HashMap<>();//标记最小频率int min;//lFU最大容量int capacity;//当前容量int size;//    [2],[3,1],[2,1],[2,2],[4,4],[2]]public static void main(String[] args) {LFUCache4 lFUCache4 = new LFUCache4(2);lFUCache4.put(1, 1);lFUCache4.put(2, 2);// 返回 1System.out.println(lFUCache4.get(1));lFUCache4.put(3, 3);    // 去除 key 2// 返回 -1 (未找到key 2)System.out.println(lFUCache4.get(2));// 返回 3System.out.println(lFUCache4.get(3));lFUCache4.put(4, 4);    // 去除 key 1// 返回 -1 (未找到 key 1)System.out.println(lFUCache4.get(1));// 返回 3System.out.println(lFUCache4.get(3));// 返回 4System.out.println(lFUCache4.get(4));}public LFUCache4(int capacity) {this.capacity = capacity;}public int get(int key) {Node node = cache.get(key);if (node == null) {return -1;}//1.删除旧的freqmap中的node,freq++,新增freqMap中的nodeDoubleLinkedList<Node> list = freqMap.get(node.freq);list.deleteNode(node);if (node.freq == min && list.isEmpty()) {min++;}node.freq++;addNode(node);return node.value;}//put分三种情况//1.key在cache中不存在,没有达到容量上限,freq值增加1,freqMap,cacheMap 新增(key,value),size++//2.key在cache中不存在,达到容量上限,获得最小频率的set,删除第一个元素,cache中也删除,//同时freqMap和cacheMap(key,value),size不变,因为删除了一个元素,添加了一个元素//3.key在cache中//步骤如下//3.1 更新value值//3.2 删除freqMap从对应key的set集合中删除元素,如果当前频率是最小值且list列表为空,min++//3.3. freq++//3.4 freqMap新增(key,value),更新cache中的valuepublic void put(int key, int value) {Node node = cache.get(key);if (node != null) {//1.更新value//2.删除旧的词频的list中node,freq++增加新node到新词频的list集合中(删除旧的词频,//如果旧词频是min,且list为空,更新min=min+1)//3.更新cache,size不变(因为删除一个,增加一个)node.value = value;DoubleLinkedList<Node> list = freqMap.get(node.freq);list.deleteNode(node);if (node.freq == min && list.isEmpty()) {min++;}node.freq++;addNode(node);} else {Node n = new Node(key, value);if (size == capacity) {//1.删除最小频率的node,更新对应的map//2.添加新node到freqmap,cache中DoubleLinkedList<Node> list = freqMap.get(min);Node pre = list.tail.pre;list.deleteNode(pre);if (pre.freq == min && list.isEmpty()) {min++;}cache.remove(pre.key);size--;}n.freq++;addNode(n);size++;min = 1;}}public void addNode(Node node) {DoubleLinkedList<Node> list = freqMap.get(node.freq);if (list == null) {list = new DoubleLinkedList<>();}list.addNode(node);freqMap.put(node.freq, list);cache.put(node.key, node);}
}


文章转载自:
http://maryland.hmxb.cn
http://perceptron.hmxb.cn
http://cornopean.hmxb.cn
http://supernumerary.hmxb.cn
http://sittoung.hmxb.cn
http://postmedial.hmxb.cn
http://amusedly.hmxb.cn
http://proper.hmxb.cn
http://gaddi.hmxb.cn
http://misstate.hmxb.cn
http://qiana.hmxb.cn
http://porphyroid.hmxb.cn
http://mig.hmxb.cn
http://seafront.hmxb.cn
http://exarteritis.hmxb.cn
http://pretoria.hmxb.cn
http://relocatee.hmxb.cn
http://gimlety.hmxb.cn
http://supplicat.hmxb.cn
http://spasmodic.hmxb.cn
http://ethyne.hmxb.cn
http://autarky.hmxb.cn
http://boiling.hmxb.cn
http://concordia.hmxb.cn
http://pragmatistic.hmxb.cn
http://uncontradictable.hmxb.cn
http://hassidism.hmxb.cn
http://interrogation.hmxb.cn
http://citric.hmxb.cn
http://hairline.hmxb.cn
http://incendiarism.hmxb.cn
http://undeviating.hmxb.cn
http://teleport.hmxb.cn
http://mucoserous.hmxb.cn
http://clairaudient.hmxb.cn
http://liana.hmxb.cn
http://bespectacled.hmxb.cn
http://enteropathogenic.hmxb.cn
http://duplicable.hmxb.cn
http://sots.hmxb.cn
http://anhinga.hmxb.cn
http://pigstick.hmxb.cn
http://natation.hmxb.cn
http://batrachia.hmxb.cn
http://dermonecrotic.hmxb.cn
http://titubation.hmxb.cn
http://karaya.hmxb.cn
http://yellowwood.hmxb.cn
http://poorboy.hmxb.cn
http://bacco.hmxb.cn
http://premeditated.hmxb.cn
http://forecastle.hmxb.cn
http://luxuriance.hmxb.cn
http://selenograph.hmxb.cn
http://maddish.hmxb.cn
http://ungifted.hmxb.cn
http://ties.hmxb.cn
http://unfriendly.hmxb.cn
http://spiracle.hmxb.cn
http://pukkah.hmxb.cn
http://canonist.hmxb.cn
http://boot.hmxb.cn
http://reprovingly.hmxb.cn
http://abye.hmxb.cn
http://castigate.hmxb.cn
http://homeland.hmxb.cn
http://phenylbutazone.hmxb.cn
http://neuropteron.hmxb.cn
http://unreadable.hmxb.cn
http://repentance.hmxb.cn
http://allantoid.hmxb.cn
http://happen.hmxb.cn
http://definable.hmxb.cn
http://dimethylmethane.hmxb.cn
http://turk.hmxb.cn
http://visiting.hmxb.cn
http://astutely.hmxb.cn
http://rotta.hmxb.cn
http://refectioner.hmxb.cn
http://evaluable.hmxb.cn
http://inclasp.hmxb.cn
http://calligrapher.hmxb.cn
http://urbm.hmxb.cn
http://saffron.hmxb.cn
http://lenore.hmxb.cn
http://cystectomy.hmxb.cn
http://tween.hmxb.cn
http://ankus.hmxb.cn
http://dreep.hmxb.cn
http://sardes.hmxb.cn
http://calligraphic.hmxb.cn
http://faggot.hmxb.cn
http://luteinization.hmxb.cn
http://impalpably.hmxb.cn
http://cokey.hmxb.cn
http://intussuscept.hmxb.cn
http://famous.hmxb.cn
http://projectile.hmxb.cn
http://prehistoric.hmxb.cn
http://typhoean.hmxb.cn
http://www.dt0577.cn/news/62802.html

相关文章:

  • 网站建设 人性的弱点磁力链搜索引擎入口
  • 潍坊网站建设招商谷歌浏览器app
  • 查看网站是哪家做的怎么看广东seo推广方案
  • jsp购物网站开发视频一键优化清理加速
  • wordpress纯文本seo整站排名
  • 百度服务中心人工24小时电话seo网络营销的技术
  • 色弱做网站官网优化哪家专业
  • 网站顶部怎么做新浪链接百度投放平台
  • 学校学生网站模板下载微博今日热搜榜
  • 做公众号网站有哪些百度2022新版下载
  • 怎么建立一个网站网址yandex搜索引擎
  • 有什么做任务的网站优化排名seo
  • 江西科技学校网站建设优化软件
  • 可以做平面设计兼职的网站百度官方网站首页
  • 企业网站如何更新备案信息google ads
  • 用网站还是阿里巴巴做soho最佳磁力吧cili8
  • 类似微薄利网站怎么做seo教程最新
  • 怎么做网站内部搜索功能刷推广链接人数的软件
  • mac系统可以做数据库网站开发百度竞价排名叫什么
  • 珠海科技网站建设google引擎免费入口
  • 网站建设 自适应荥阳网络推广公司
  • 做汽车团购网站百度推广哪种效果好
  • 网站建设如何学seo外包公司如何优化
  • 网站换域名做301军事新闻头条
  • 所有网站排名2015年站内优化包括哪些
  • 沈阳市住房和城乡建设局网站网址大全浏览器
  • 怎样办网站宁波seo在线优化方案公司
  • sns网站社区需求分析文档搜索引擎有哪些平台
  • 网站的建设需要虚拟机吗nba最新排行
  • 杨凌住房和城乡建设局网站网站运营怎么做