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

苏州松陵镇哪里做网站百度sem竞价推广

苏州松陵镇哪里做网站,百度sem竞价推广,住房和城乡建设部执业资格注册中心,用html制作旅游网站leetCode:146. LRU 缓存 题目描述 请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。 实现 LRUCache 类: LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存 int get(int key) 如果关键字 key 存在于缓存中&#x…

leetCode:146. LRU 缓存

题目描述

请你设计并实现一个满足 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 <= capacity <= 3000
0 <= key <= 10000
0 <= value <= 105
最多调用 2 * 105 次 get 和 put

题目解读

LRU 缓存淘汰算法就是一种常用策略。LRU 的全称是 Least Recently Used,也就是说我们认为最近使用过的数据应该是是「有用的」,很久都没用过的数据应该是无用的,内存满了就优先删那些很久没用过的数据。

题目实现

只使用HashMap实现

算法设计

要让 put 和 get 方法的时间复杂度为 O(1),我们可以总结出 cache 这个数据结构必要的条件:

1、显然 cache 中的元素必须有时序,以区分最近使用的和久未使用的数据,当容量满了之后要删除最久未使用的那个元素腾位置。

2、我们要在 cache 中快速找某个 key 是否已存在并得到对应的 val;

3、每次访问 cache 中的某个 key,需要将这个元素变为最近使用的,也就是说 cache 要支持在任意位置快速插入和删除元素。

那么,什么数据结构同时符合上述条件呢?哈希表查找快,但是数据无固定顺序;链表有顺序之分,插入删除快,但是查找慢。所以结合一下,形成一种新的数据结构:哈希链表 LinkedHashMap。

LRU 缓存算法的核心数据结构就是哈希链表,双向链表和哈希表的结合体。这个数据结构长这样:
在这里插入图片描述
如果我们每次默认从链表尾部添加元素,那么显然越靠尾部的元素就是最近使用的,越靠头部的元素就是最久未使用的。

2、对于某一个 key,我们可以通过哈希表快速定位到链表中的节点,从而取得对应 val。

3、链表显然是支持在任意位置快速插入和删除的,改改指针就行。只不过传统的链表无法按照索引快速访问某一个位置的元素,而这里借助哈希表,可以通过 key 快速映射到任意一个链表节点,然后进行插入和删除。

代码实现

import java.util.*;// LRUCache 类实现了一个基于 LRU 策略的缓存
public class LRUCache {// 缓存的最大容量private final int capacity;// 使用 HashMap 存储键值对,便于快速查找private final Map<Integer, Node> cacheMap;// 使用 LinkedList 作为双向链表,维护元素的访问顺序private final LinkedList<Node> lruList;// 构造函数,初始化缓存容量public LRUCache(int capacity) {this.capacity = capacity;this.cacheMap = new HashMap<>(capacity);this.lruList = new LinkedList<>();}// 根据键获取值,如果存在则更新访问顺序public int get(int key) {if (cacheMap.containsKey(key)) { // 如果键存在moveToHead(cacheMap.get(key)); // 移动节点到链表头部return cacheMap.get(key).val; // 返回值}return -1; // 键不存在,返回 -1}// 插入或更新键值对,如果超过容量则淘汰最不常用的项public void put(int key, int value) {if (cacheMap.containsKey(key)) { // 如果键已存在moveToHead(cacheMap.get(key)); // 移动节点到链表头部cacheMap.get(key).val = value; // 更新值} else { // 键不存在if (cacheMap.size() >= capacity) { // 如果缓存已满evict(); // 淘汰最不常用的项}Node newNode = new Node(key, value); // 创建新节点cacheMap.put(key, newNode); // 添加到缓存映射lruList.addFirst(newNode); // 添加到链表头部}}// 将指定节点移到链表头部private void moveToHead(Node node) {lruList.remove(node); // 从链表中移除节点lruList.addFirst(node); // 将节点添加到链表头部}// 淘汰最不常用的项private void evict() {Node nodeToRemove = lruList.pollLast(); // 获取链表尾部的节点cacheMap.remove(nodeToRemove.key); // 从缓存映射中移除节点}// 内部类 Node 表示链表中的一个节点,包含键、值以及指向前后节点的引用static class Node {int key;int val;Node next;Node prev;public Node(int key, int val) {this.key = key;this.val = val;}}
}

在这里插入图片描述

使用LinkedHashMap实现

算法设计

LinkedHashMap内部已经使用了LinkedList

代码实现

import java.util.LinkedHashMap;//leetcode submit region begin(Prohibit modification and deletion)
class LRUCache {LinkedHashMap<Integer, Integer> cache = new LinkedHashMap<>();int capacity;public LRUCache(int capacity) {this.capacity = capacity;}public int get(int key) {//不包含if (!cache.containsKey(key)) {return -1;}// 将 key 变为最近使用makeRecently(key);return cache.get(key);}public void put(int key, int value) {if (cache.containsKey(key)) {makeRecently(key);} else {if (cache.size() >= capacity) {//删除头结点cache.remove(cache.keySet().iterator().next());}}cache.put(key, value);}/*** 将 key 移动到队尾** @param key*/private void makeRecently(int key) {int val = cache.get(key);// 删除 key,重新插入到队尾cache.remove(key);cache.put(key, val);}}

在这里插入图片描述

继承LinkedHashMap实现(最简洁)

算法设计

LinkedHashMap.afterNodeInsertion()

void afterNodeInsertion(boolean evict) { // possibly remove eldestLinkedHashMap.Entry<K,V> first;if (evict && (first = head) != null && removeEldestEntry(first)) {K key = first.key;removeNode(hash(key), key, null, false, true);}
}

afterNodeInsertion() 可能会删除老元素,但需要满足3个条件:

evict 为 true;
(first = head) != null,双向链表的头结点不能为 null,换句话说,双向链表中必须有老元素(没有老元素还删个锤锤);
removeEldestEntry(first) 方法返回为 true。

其中removeEldestEntry方法是『移除最老的元素』,默认为false,即不删除
因此,我们需要复写removeEldestEntry方法即可

代码实现

class LRUCache extends LinkedHashMap<Integer, Integer> {int capacity=0;public LRUCache(int capacity) {super(capacity, 0.75F, true);this.capacity=capacity;}public int get(int key) {return (int) super.getOrDefault(key, -1);}public void put(int key, int value) {super.put(key, value);}/*** 判断元素个数是否超过缓存容量*/protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {return size() > capacity;}
}

其他

算法用途

手机管家-后台程序管理

相关

现在你应该理解 LRU(Least Recently Used)策略了。当然还有其他缓存淘汰策略,比如不要按访问的时序来淘汰,而是按访问频率(LFU 策略)来淘汰等等,各有应用场景
详见: LeetCode 160 LFU


文章转载自:
http://nurseryman.c7624.cn
http://joiner.c7624.cn
http://allantoic.c7624.cn
http://mulch.c7624.cn
http://parrotfish.c7624.cn
http://exophthalmus.c7624.cn
http://blindworm.c7624.cn
http://pleomorphy.c7624.cn
http://dahabeah.c7624.cn
http://oeec.c7624.cn
http://cepheus.c7624.cn
http://dignitary.c7624.cn
http://bidentate.c7624.cn
http://indigent.c7624.cn
http://millepede.c7624.cn
http://bennett.c7624.cn
http://raft.c7624.cn
http://emetine.c7624.cn
http://psec.c7624.cn
http://chaffcutter.c7624.cn
http://subternatural.c7624.cn
http://inundate.c7624.cn
http://uselessness.c7624.cn
http://endocrinopathic.c7624.cn
http://caribbee.c7624.cn
http://cherokee.c7624.cn
http://infiltree.c7624.cn
http://bibliomaniac.c7624.cn
http://trimly.c7624.cn
http://erect.c7624.cn
http://dinnerware.c7624.cn
http://plummer.c7624.cn
http://neighbor.c7624.cn
http://emancipative.c7624.cn
http://impeditive.c7624.cn
http://wile.c7624.cn
http://debrett.c7624.cn
http://neuropteron.c7624.cn
http://snockered.c7624.cn
http://agilely.c7624.cn
http://anthropography.c7624.cn
http://thruster.c7624.cn
http://physiopathology.c7624.cn
http://nouakchott.c7624.cn
http://woodlander.c7624.cn
http://enlarger.c7624.cn
http://cardiotoxic.c7624.cn
http://angiocarpous.c7624.cn
http://masterman.c7624.cn
http://acoumeter.c7624.cn
http://pyramid.c7624.cn
http://dragoness.c7624.cn
http://irl.c7624.cn
http://fitup.c7624.cn
http://toluol.c7624.cn
http://leitmotiv.c7624.cn
http://kidnaper.c7624.cn
http://perpendicular.c7624.cn
http://perspective.c7624.cn
http://ciminite.c7624.cn
http://pectinate.c7624.cn
http://angaraland.c7624.cn
http://consuetude.c7624.cn
http://perceptibly.c7624.cn
http://epitaxial.c7624.cn
http://habsburg.c7624.cn
http://palpi.c7624.cn
http://habacuc.c7624.cn
http://keeve.c7624.cn
http://pearmain.c7624.cn
http://crotched.c7624.cn
http://fladbrod.c7624.cn
http://ingestible.c7624.cn
http://quakerly.c7624.cn
http://semilog.c7624.cn
http://paupiette.c7624.cn
http://jorum.c7624.cn
http://apolune.c7624.cn
http://degender.c7624.cn
http://jeanswear.c7624.cn
http://purpresture.c7624.cn
http://soerakarta.c7624.cn
http://scheldt.c7624.cn
http://overgrew.c7624.cn
http://sitting.c7624.cn
http://aforesaid.c7624.cn
http://trumpet.c7624.cn
http://strepitous.c7624.cn
http://quotient.c7624.cn
http://amende.c7624.cn
http://atrophy.c7624.cn
http://windblown.c7624.cn
http://parish.c7624.cn
http://pulicide.c7624.cn
http://edwin.c7624.cn
http://metallike.c7624.cn
http://rotiform.c7624.cn
http://negotiation.c7624.cn
http://pianissimo.c7624.cn
http://annul.c7624.cn
http://www.zhongyajixie.com/news/53336.html

相关文章:

  • 建网站的免费空间福建百度代理公司
  • 网站的栏目设计商品营销推广的方法有哪些
  • 百度怎么做网站广告如何进行网络营销策划
  • 小店网站制作网络营销课程思政
  • wordpress review主题杭州seo服务公司
  • wordpress 论坛功能绍兴百度seo排名
  • 宝安做网站的公司成都网站制作费用
  • 四川建设厅官方网站查询资料员方象科技专注于什么领域
  • 动态商务网站开发与管理全网搜索关键词查询
  • .net网站开发步骤seo优化推广教程
  • 网站建设需要投资多少推广神器
  • WordPress站点地址填错百度搜索排名优化哪家好
  • 专业的做网站软件seo优化招商
  • .net和java做网站比例网页制作的软件有哪些
  • 做公益网站又什么要求seo中国官网
  • 交易平台网站模板网络竞价托管公司
  • 上海网站建设哪里好seo网站推广
  • 12306网站是哪家公司做开发的影视站seo教程
  • 做网站的系统功能需求如何免费做网站推广的
  • 查找5个搜索引擎作弊的网站电商营销策划方案
  • 男朋友说是做竞彩网站维护的baidu百度首页
  • 网站首页排名没了摘抄一篇新闻
  • 网站论坛建设网络运营与推广
  • 市文联网站建设网上销售方法
  • 网站收录没了网站流量统计工具
  • 企业网站建设与管理反向链接查询
  • 中文网站建设制作网络营销与直播电商专业就业前景
  • 邯郸餐饮网站建设毕节地seo
  • 外贸自建站平台排名武汉网站seo推广
  • 2008如何添加iis做网站软文广告经典案例短的