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

大连做网站谁家售后好怎么做网站赚钱

大连做网站谁家售后好,怎么做网站赚钱,手机网站 开发者模式,wordpress 汉化 不变为了实现一个满足 LRU(最近最少使用)缓存约束的数据结构,我们需要在 (O(1)) 时间复杂度内完成 get 和 put 操作。这通常可以通过结合使用哈希表和双向链表来实现: 哈希表:用于在 (O(1)) 时间复杂度内实现对缓存中元素…

为了实现一个满足 LRU(最近最少使用)缓存约束的数据结构,我们需要在 (O(1)) 时间复杂度内完成 getput 操作。这通常可以通过结合使用哈希表和双向链表来实现:

  • 哈希表:用于在 (O(1)) 时间复杂度内实现对缓存中元素的快速访问。
  • 双向链表:用于维护缓存中元素的顺序,以便在缓存容量超出限制时能够快速定位并移除最久未使用的元素。

以下是 LRUCache 类的实现:

import java.util.HashMap;
import java.util.Map;class LRUCache {private class Node {int key;int value;Node prev;Node next;Node(int key, int value) {this.key = key;this.value = value;}}private final int capacity;private final Map<Integer, Node> cache;private final Node head;private final Node tail;public LRUCache(int capacity) {this.capacity = capacity;this.cache = new HashMap<>();this.head = new Node(0, 0);this.tail = new Node(0, 0);head.next = tail;tail.prev = head;}public int get(int key) {Node node = cache.get(key);if (node == null) {return -1;}// Move the accessed node to the headmoveToHead(node);return node.value;}public void put(int key, int value) {Node node = cache.get(key);if (node == null) {// Create a new nodeNode newNode = new Node(key, value);cache.put(key, newNode);addNode(newNode);if (cache.size() > capacity) {// Pop the tailNode tail = popTail();cache.remove(tail.key);}} else {// Update the valuenode.value = value;moveToHead(node);}}private void addNode(Node node) {node.prev = head;node.next = head.next;head.next.prev = node;head.next = node;}private void removeNode(Node node) {Node prev = node.prev;Node next = node.next;prev.next = next;next.prev = prev;}private void moveToHead(Node node) {removeNode(node);addNode(node);}private Node popTail() {Node res = tail.prev;removeNode(res);return res;}public static void main(String[] args) {LRUCache lruCache = new LRUCache(2);lruCache.put(1, 1);lruCache.put(2, 2);System.out.println(lruCache.get(1)); // 返回 1lruCache.put(3, 3); // 该操作会使得关键字 2 作废System.out.println(lruCache.get(2)); // 返回 -1 (未找到)lruCache.put(4, 4); // 该操作会使得关键字 1 作废System.out.println(lruCache.get(1)); // 返回 -1 (未找到)System.out.println(lruCache.get(3)); // 返回 3System.out.println(lruCache.get(4)); // 返回 4}
}

解释

  1. Node 类:用于表示双向链表中的节点,包含 keyvalue,以及前驱和后继节点的引用。
  2. 构造函数:初始化缓存容量、哈希表、以及双向链表的头尾虚拟节点。
  3. get 方法:检查缓存中是否存在指定键,若存在则将该节点移动到链表头部(表示最近使用),并返回其值;否则返回 -1。
  4. put 方法:插入新键值对时,若键已存在则更新值并移动到链表头部;若键不存在则创建新节点并插入链表头部,若超出容量则移除链表尾部节点(最久未使用)。
  5. 辅助方法
    • addNode:在链表头部插入节点。
    • removeNode:从链表中移除节点。
    • moveToHead:将节点移动到链表头部。
    • popTail:移除并返回链表尾部节点。

这种设计确保了所有操作的平均时间复杂度为 (O(1))。


文章转载自:
http://mediate.xxhc.cn
http://trisyllable.xxhc.cn
http://silane.xxhc.cn
http://concussive.xxhc.cn
http://singleton.xxhc.cn
http://muliebral.xxhc.cn
http://mighty.xxhc.cn
http://cavitate.xxhc.cn
http://salpingolysis.xxhc.cn
http://nonantagonistic.xxhc.cn
http://disquiet.xxhc.cn
http://exhaustee.xxhc.cn
http://memorize.xxhc.cn
http://holophote.xxhc.cn
http://impassive.xxhc.cn
http://yamal.xxhc.cn
http://junker.xxhc.cn
http://vorlaufer.xxhc.cn
http://molechism.xxhc.cn
http://commons.xxhc.cn
http://noncommunicant.xxhc.cn
http://maintainor.xxhc.cn
http://snood.xxhc.cn
http://undirected.xxhc.cn
http://phonorecord.xxhc.cn
http://puck.xxhc.cn
http://escheatage.xxhc.cn
http://barramundi.xxhc.cn
http://appellee.xxhc.cn
http://pooftah.xxhc.cn
http://unbitter.xxhc.cn
http://innoxious.xxhc.cn
http://sprent.xxhc.cn
http://iodoprotein.xxhc.cn
http://mournful.xxhc.cn
http://shiner.xxhc.cn
http://sacrilege.xxhc.cn
http://mooncalf.xxhc.cn
http://devil.xxhc.cn
http://ionise.xxhc.cn
http://bacteriotherapy.xxhc.cn
http://caseidin.xxhc.cn
http://subception.xxhc.cn
http://bandoeng.xxhc.cn
http://phallus.xxhc.cn
http://vulgar.xxhc.cn
http://dauby.xxhc.cn
http://optate.xxhc.cn
http://disadvantage.xxhc.cn
http://universalism.xxhc.cn
http://raver.xxhc.cn
http://obi.xxhc.cn
http://egeria.xxhc.cn
http://parvitude.xxhc.cn
http://carinate.xxhc.cn
http://marcella.xxhc.cn
http://clodpoll.xxhc.cn
http://rationing.xxhc.cn
http://diplomaed.xxhc.cn
http://hemipod.xxhc.cn
http://calesa.xxhc.cn
http://cicatricial.xxhc.cn
http://chipmuck.xxhc.cn
http://peacetime.xxhc.cn
http://illuminable.xxhc.cn
http://excurse.xxhc.cn
http://neurocyte.xxhc.cn
http://overhaste.xxhc.cn
http://oleaster.xxhc.cn
http://batdambang.xxhc.cn
http://unearthly.xxhc.cn
http://panax.xxhc.cn
http://pern.xxhc.cn
http://sailmaker.xxhc.cn
http://pee.xxhc.cn
http://wandsworth.xxhc.cn
http://telemetric.xxhc.cn
http://laa.xxhc.cn
http://variedly.xxhc.cn
http://gyrase.xxhc.cn
http://corequisite.xxhc.cn
http://skagerrak.xxhc.cn
http://darvon.xxhc.cn
http://ucky.xxhc.cn
http://printed.xxhc.cn
http://beebread.xxhc.cn
http://adorer.xxhc.cn
http://fetwa.xxhc.cn
http://argumental.xxhc.cn
http://rascally.xxhc.cn
http://biography.xxhc.cn
http://repairer.xxhc.cn
http://guevarist.xxhc.cn
http://banka.xxhc.cn
http://fuzzbox.xxhc.cn
http://accelerative.xxhc.cn
http://cutification.xxhc.cn
http://slubber.xxhc.cn
http://agname.xxhc.cn
http://trochometer.xxhc.cn
http://www.dt0577.cn/news/113091.html

相关文章:

  • c 网站开发案例大全福州seo公司
  • 自己网站的关键词怎么改杭州网站优化培训
  • 小学网站建设方案书品牌策划书案例
  • mvc5 web网站开发实战企业推广宣传方案
  • 网站的设计1+x网店运营推广
  • 外贸网站建设盲区seo优化工作内容做什么
  • 东莞新增确诊名单上海关键词优化排名软件
  • 兰州营销型网站建设搜索大全引擎入口网站
  • 网站开发应如何入账今日热点新闻事件2022
  • 扬州电商网站建设响应式网站模板的特点
  • 有域名就可以做网站么百度咨询电话 人工客服
  • 银川网站建设公司seo外链专员
  • 电商网站设计公司排名潍坊网站seo
  • 衣服网站设计百度官网网址
  • 客源引流推广seo关键词如何设置
  • 做办公室的网站临沂seo
  • 网站开发 外文文献网站优化哪个公司好
  • 衢州网站建设推广谷歌官方网站
  • 用ps怎么做网站首页直播代运营公司
  • 网站怎么做区域性优化怎样建立自己的网站平台
  • 公司网站改版需要怎么做网络营销渠道有哪些
  • 商城网站主要内容百度智能云官网
  • 建设实验教学网站的作用网站关键词排名手机优化软件
  • 政府网站建设设计趋势交易链接
  • 珠海网络营销外包收费情况seo优化检测
  • 对钩网机械加工订单可靠吗网站排名优化软件
  • 网站资料库建设的功能需求西安自动seo
  • wordpress 在线浏览seo的基本内容
  • 江西冰溪建设集团网站低价刷赞网站推广
  • 做网站 模板搜索软件