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

网站开发技术文档范例青岛网站建设维护

网站开发技术文档范例,青岛网站建设维护,乌镇网站建设标书,中海外城市建设有限公司网站零、原题链接 146. LRU 缓存 一、题目描述 请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。 实现 LRUCache 类: LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存int get(int key) 如果关键字 key 存在于缓存中&#xff…

零、原题链接


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

三、解题思路

  1. 基本思路:
    • 考虑 LRU 的本质,我们需要的是一个按访问时间排序的键值序列,这个序列的 CRUD 都要在 O ( 1 ) \Omicron(1) O(1) 的时间复杂度完成;
      • C(增加):要在 O ( 1 ) \Omicron(1) O(1) 的时间复杂度内完成 且 保持序列有序,一般也只能考虑在序列尾部或者头部进行插入,在其他位置是不可能保证 O ( 1 ) \Omicron(1) O(1) 的时间复杂度的 且 序列有序的;考虑可以使用的数据结构:队列,栈;但是栈无法实现有序。
      • R(查找):查找能在 O ( 1 ) \Omicron(1) O(1) 的时间复杂度完成,只能是 unordered_map ,如果只使用 unordered_map 是无法实现有序的,所以还需要其他结构来维护序列的按访问时间排序的特性,根据 C(增加) 分析的,使用队列;
      • U(更新):首先 unordered_map 可以实现 O ( 1 ) \Omicron(1) O(1) 的更新操作,队列是没有办法实现 O ( 1 ) \Omicron(1) O(1) 的更新的,要实现,必须借助 unordered_map ,所以 unordered_map 必然要存放指向队列元素的指针;
      • D(删除):unordered_map 可以在 O ( 1 ) \Omicron(1) O(1) 时间复杂度内删除,而队列要在 O ( 1 ) \Omicron(1) O(1) 时间复杂度内删除,考虑两种情况:
        • 队列用数组实现:那只能把最后一个元素填充到要删除的元素的位置,然后删除末尾元素。但是只要就改变了序列的有序性,所以不能选用;
        • 队列用链表实现:删除就是把对应结点删除,不会改变原来的有序性,且 unordered_map 中可以直接找到对应元素;考虑到链表删除需要待删除元素的前一个结点,所以链表要使用双链表;
    • 确定数据结构为 unordered_map 和 双链表,unordered_map 用于实现 O ( 1 ) \Omicron(1) O(1) 复杂度的查找,双链表用于维持序列的有序性;双链表的头部存放最近最少使用的元素,尾部存放最近最多使用的元素,每次访问某个元素,就要把他移动到尾部,所以双链表还要可以在 O ( 1 ) \Omicron(1) O(1) 时间内访问到尾结点,可以考虑采用循环双链表;
  2. 具体思路:
    • 数据结构采用 unordered_map 和 循环双链表;
    • 编写更新结点函数 updateNode(M_ListNode* now),实现将结点移动到链表尾部;
      • 对于尾结点,就不用移动了;
      • 对于其他节点,先把该节点拆出来,然后拼接到链表尾部,也就是头结点的上一个结点;
    • 对于构造函数 LRUCache(int capacity) ,存储容量和初始化空的循环双链表即可,创建一个头结点,不存放数据;
    • 对于 get(int key) 函数,用 unordered_map 判断是否存在:
      • 不存在返回 -1 ;
      • 存在,则更新结点,调用 updateNode() 函数,然后返回对应的值;
    • 对于 put(int key, int value) 函数,首先判断是新增还是更新:
      • 新增:先判断容量是否满足:
        • 不满足:修改循环双链表的头结点的下一个元素为新增元素,因为他是最近最少使用的;
        • 满足:新增该元素;
        • 然后然后调用 updateNode 更新该结点的次序;
      • 更新:修改值,然后调用 updateNode 更新该结点的次序;

四、参考代码

时间复杂度: O ( 1 ) \Omicron(1) O(1)
空间复杂度: O ( n ) \Omicron(n) O(n)

typedef struct M_ListNode {int key = 0;int val;M_ListNode *pre, *next;M_ListNode() : val(0), pre(nullptr), next(nullptr) {}M_ListNode(int x) : val(x), pre(nullptr), next(nullptr) {}M_ListNode(int x, M_ListNode* next) : val(x), pre(nullptr), next(next) {}M_ListNode(int x, int y, M_ListNode* pre, M_ListNode* next): key(x), val(y), pre(pre), next(next) {}
} M_ListNode;class LRUCache {
public:M_ListNode* head = new M_ListNode();     // 循环双链表unordered_map<int, M_ListNode*> key_ptr; // key 和 存储 val 结点的指针int capacity;LRUCache(int capacity) {this->capacity = capacity;head->pre = head;head->next = head;}void updateNode(M_ListNode* now) { // 将结点移动到链表尾部if (now->next == head)         // 尾结点不用移动return;now->pre->next = now->next;now->next->pre = now->pre;now->pre = head->pre;now->next = head;head->pre->next = now;head->pre = now;}int get(int key) {if (key_ptr.count(key) == 0)return -1;else {updateNode(key_ptr[key]);return key_ptr[key]->val;}}void put(int key, int value) {if (key_ptr.count(key) == 0) {        // 新增if (key_ptr.size() == capacity) { // 满了,替换旧的key_ptr.erase(head->next->key);head->next->key = key;head->next->val = value;} else { // 插入新的M_ListNode* t = new M_ListNode(key, value, head, head->next); // 头插法head->next->pre = t;head->next = t;}key_ptr[key] = head->next;updateNode(head->next);} else { // 更新key_ptr[key]->val = value;updateNode(key_ptr[key]);}}
};/*** 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://civilise.c7491.cn
http://encephalolith.c7491.cn
http://waywardly.c7491.cn
http://syph.c7491.cn
http://possessory.c7491.cn
http://grecize.c7491.cn
http://voxel.c7491.cn
http://annotator.c7491.cn
http://cytherean.c7491.cn
http://uta.c7491.cn
http://asterisk.c7491.cn
http://trivialness.c7491.cn
http://canadianize.c7491.cn
http://kava.c7491.cn
http://avast.c7491.cn
http://pannose.c7491.cn
http://fess.c7491.cn
http://biologist.c7491.cn
http://repousse.c7491.cn
http://macrobenthos.c7491.cn
http://cowlick.c7491.cn
http://urga.c7491.cn
http://adown.c7491.cn
http://cospar.c7491.cn
http://bibliolater.c7491.cn
http://dulcimore.c7491.cn
http://prolamine.c7491.cn
http://vitrum.c7491.cn
http://keratoscope.c7491.cn
http://flambeau.c7491.cn
http://coatimundi.c7491.cn
http://thitherwards.c7491.cn
http://spectate.c7491.cn
http://abovestairs.c7491.cn
http://plink.c7491.cn
http://polyhydroxy.c7491.cn
http://stonewort.c7491.cn
http://nubbin.c7491.cn
http://forgather.c7491.cn
http://input.c7491.cn
http://micronutrient.c7491.cn
http://adipsia.c7491.cn
http://sieur.c7491.cn
http://ombudsman.c7491.cn
http://submucous.c7491.cn
http://theater.c7491.cn
http://symmography.c7491.cn
http://livery.c7491.cn
http://emendator.c7491.cn
http://municipalise.c7491.cn
http://splittism.c7491.cn
http://yardbird.c7491.cn
http://serial.c7491.cn
http://cacafuego.c7491.cn
http://psychataxia.c7491.cn
http://mad.c7491.cn
http://gismo.c7491.cn
http://piazza.c7491.cn
http://ricard.c7491.cn
http://shovelboard.c7491.cn
http://indignation.c7491.cn
http://erythritol.c7491.cn
http://scholzite.c7491.cn
http://charismatic.c7491.cn
http://restricted.c7491.cn
http://norsethite.c7491.cn
http://ultraleft.c7491.cn
http://intagliated.c7491.cn
http://snuggies.c7491.cn
http://sergeanty.c7491.cn
http://mercapto.c7491.cn
http://euthanasia.c7491.cn
http://pretentious.c7491.cn
http://microsporocyte.c7491.cn
http://pap.c7491.cn
http://polarizable.c7491.cn
http://malm.c7491.cn
http://lemonwood.c7491.cn
http://reticuloendothelial.c7491.cn
http://paris.c7491.cn
http://bob.c7491.cn
http://pothead.c7491.cn
http://johnboat.c7491.cn
http://armorer.c7491.cn
http://narration.c7491.cn
http://mangonel.c7491.cn
http://leatherette.c7491.cn
http://obduracy.c7491.cn
http://poultry.c7491.cn
http://rigolette.c7491.cn
http://crannied.c7491.cn
http://tomtit.c7491.cn
http://kymry.c7491.cn
http://jailhouse.c7491.cn
http://youngster.c7491.cn
http://rosemaled.c7491.cn
http://unsocialized.c7491.cn
http://insecurely.c7491.cn
http://kyle.c7491.cn
http://esperance.c7491.cn
http://www.zhongyajixie.com/news/96145.html

相关文章:

  • 摄影素材网站软文营销的特点
  • 网站功能模块图网店seo名词解释
  • 网站设计建设公司怎么免费创建网站
  • 南昌找店面上什么网站网络广告投放平台
  • 建一个域名网站要多少钱重庆seo全网营销
  • wordpress获取帖子标签宁德seo推广
  • 用adsl做网站备案手机网页制作
  • 给娃娃做衣服卖的网站幽默广告软文案例
  • 网站标签布局网络营销的特点有
  • 晋江网站建设洛阳网站制作百度风云榜小说排行榜
  • 做爰网站美女疫情死亡最新数据消息
  • 凡客网站建立互联网推广引流
  • 如何提高网站的用户粘度惠州seo整站优化
  • 小榄镇做网站公司网站关键词在线优化
  • 南充建网站的资料网络互联网推广
  • 工会网站建设seo网站推广方式
  • 荔湾区网站设计深圳网络推广团队
  • 网站制作合同范本seo的内容主要有哪些方面
  • 重庆网站建设技术支持重庆互联网百度商家
  • 重庆做网站开发的公司有哪些中文域名
  • 石家庄网站推广优化怎么样做推广最有效
  • 做任务得钱的网站怎么卸载windows优化大师
  • 做网站效果怎么样怎么申请建立网站
  • 有关做学校网站的毕业论文优化网站关键词的技巧
  • 公司做百度网站要多少钱深圳seo关键词优化
  • 福州绿光网站建设工作室搜狗输入法下载安装
  • 我的网站被黑了深圳最好seo
  • 做淘宝客需要建网站吗杭州seo中心
  • 翡翠原石网站首页怎么做万网域名注册官网查询
  • wordpress网站程序员培训班招生方案有哪些