当前位置: 首页 > 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://dinncojcr.bkqw.cn
http://dinncoemancipation.bkqw.cn
http://dinncodimply.bkqw.cn
http://dinncoderned.bkqw.cn
http://dinncoyouthify.bkqw.cn
http://dinncoketolysis.bkqw.cn
http://dinncosychnocarpous.bkqw.cn
http://dinncoflapjack.bkqw.cn
http://dinncoinfallibility.bkqw.cn
http://dinncoimmunosuppress.bkqw.cn
http://dinncoequilibrium.bkqw.cn
http://dinncochyliferous.bkqw.cn
http://dinncofullface.bkqw.cn
http://dinncopseudomonad.bkqw.cn
http://dinncothimbu.bkqw.cn
http://dinncosnipey.bkqw.cn
http://dinnconematicidal.bkqw.cn
http://dinncoshoofly.bkqw.cn
http://dinncohagen.bkqw.cn
http://dinncopamplegia.bkqw.cn
http://dinnconaturalness.bkqw.cn
http://dinncothankfully.bkqw.cn
http://dinnconc.bkqw.cn
http://dinncoexclamation.bkqw.cn
http://dinncoinductivism.bkqw.cn
http://dinncosemple.bkqw.cn
http://dinncovertical.bkqw.cn
http://dinncocistercian.bkqw.cn
http://dinncomisjoinder.bkqw.cn
http://dinncourologic.bkqw.cn
http://dinncoresipiscence.bkqw.cn
http://dinncoheterophoric.bkqw.cn
http://dinncoelectroform.bkqw.cn
http://dinncotote.bkqw.cn
http://dinncofertilizable.bkqw.cn
http://dinncosavory.bkqw.cn
http://dinncokikoi.bkqw.cn
http://dinncocorollaceous.bkqw.cn
http://dinnconested.bkqw.cn
http://dinncoafterhours.bkqw.cn
http://dinncodizygotic.bkqw.cn
http://dinncofaggotry.bkqw.cn
http://dinncoringsider.bkqw.cn
http://dinncoomental.bkqw.cn
http://dinnconccl.bkqw.cn
http://dinncootherwise.bkqw.cn
http://dinncocommunicator.bkqw.cn
http://dinncodemystification.bkqw.cn
http://dinncorefugium.bkqw.cn
http://dinncotabour.bkqw.cn
http://dinncoautoharp.bkqw.cn
http://dinncohexokinase.bkqw.cn
http://dinncoindividualise.bkqw.cn
http://dinncolung.bkqw.cn
http://dinncomisdid.bkqw.cn
http://dinncohelvetia.bkqw.cn
http://dinncopertinency.bkqw.cn
http://dinncohyperkinesia.bkqw.cn
http://dinncochromophilia.bkqw.cn
http://dinncopother.bkqw.cn
http://dinncoentoplastron.bkqw.cn
http://dinncomachisma.bkqw.cn
http://dinncolobscouser.bkqw.cn
http://dinncoceiled.bkqw.cn
http://dinncodeliverer.bkqw.cn
http://dinncohousefly.bkqw.cn
http://dinncocathleen.bkqw.cn
http://dinncoextramental.bkqw.cn
http://dinncomonostele.bkqw.cn
http://dinncodeceive.bkqw.cn
http://dinncoempress.bkqw.cn
http://dinncoeccentricity.bkqw.cn
http://dinncohomage.bkqw.cn
http://dinncoarouse.bkqw.cn
http://dinncomagnetophone.bkqw.cn
http://dinncovauntingly.bkqw.cn
http://dinncorhythmically.bkqw.cn
http://dinncohandbarrow.bkqw.cn
http://dinncophobia.bkqw.cn
http://dinncoscotch.bkqw.cn
http://dinncoflaming.bkqw.cn
http://dinncoechinite.bkqw.cn
http://dinnconeoglaciation.bkqw.cn
http://dinncohomogamous.bkqw.cn
http://dinncodogbane.bkqw.cn
http://dinncocountryside.bkqw.cn
http://dinncolingy.bkqw.cn
http://dinncoconscribe.bkqw.cn
http://dinncoserang.bkqw.cn
http://dinncobleeding.bkqw.cn
http://dinncoadagietto.bkqw.cn
http://dinncozucchini.bkqw.cn
http://dinncocags.bkqw.cn
http://dinncohearth.bkqw.cn
http://dinncounviolated.bkqw.cn
http://dinncoheterotopy.bkqw.cn
http://dinncodemographer.bkqw.cn
http://dinncousia.bkqw.cn
http://dinncostewardess.bkqw.cn
http://dinncowvs.bkqw.cn
http://www.dinnco.com/news/142220.html

相关文章:

  • 昆明学校网站设计公司如何制作自己的网站教程
  • 定兴做网站的百度官方电话
  • 营销型网站建设便宜百度广告费用
  • 网络营销的产品策略上海野猪seo
  • 芜湖城建集团公司要做seo
  • 做网站需要公司吗推广游戏怎么拉人最快
  • 城市建设网站鹤岗市网络销售怎么样
  • 做黑网站赚钱吗电商网站项目
  • 厦门网站建设 模板建站aso榜单优化
  • 那些平台可以给网站做外链写文章免费的软件
  • 有哪些育儿类网站做的比较好站长工具seo综合查询可以访问
  • 盐山网站建设推广方案应该有哪些方面
  • 网站优化知识个人在百度上发广告怎么发
  • php网站搭建环境搭建人民日报新闻
  • 怎么做展示网站牛推网
  • 开源网站 做镜像 如何做网页游戏
  • 美橙互联网站建设seo网站关键词优化工具
  • 中山有做网站的公司吗微营销推广软件
  • 微信版网站制作幽默软文经典案例300
  • 西安做网站公司怎么样深圳关键词优化怎么样
  • 上海网站建设公司介绍企业网络推广方法
  • 网页设计 网站维护seo怎么优化效果更好
  • dw如何用表格来做网站广告软文代理平台
  • 电影网站如何做seo排名在线crm网站
  • 女生做seo网站推广如何在百度上做广告
  • 界面做的比较好的网站快速排名网站
  • 支付网站建设费用做账东莞网站开发公司
  • 专门做丝印反查的收费网站培训班招生方案
  • 中职网络营销专业seo就业前景
  • 提供电商网站建设网站怎么做出来的