LintCode 134: LRU Cache (经典题)

CTFmisc-------Jack 每一个文件里面都有不一样的base64编码,我尝试将其解密,发现解出来的是乱码,所以我想到真正的flag藏在这些字符串中,所以我们要写代码把字符串提取出来。提取出来后,得到很多的base64的编码,要一个一个的解码很麻烦,猜到里面有flag字样的base64编码,所以我们将flag进行base64编码,1.下载得到Jack文件夹,里面有很多的txt文件。发现这个字符串是众多编码中唯一一个没有==然后从那些base编码中寻找Zmxh字段。 阅读详情
  1. LRU Cache
    中文English
    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item. Finally, you need to return the data from each get

Example
Example1

Input:
LRUCache(2)
set(2, 1)
set(1, 1)
get(2)
set(4, 1)
get(1)
get(2)
Output: [1,-1,1]
Explanation:
cache cap is 2,set(2,1),set(1, 1),get(2) and return 1,set(4,1) and delete (1,1),because (1,1)is the least use,get(1) and return -1,get(2) and return 1.
Example 2:

Input:
LRUCache(1)
set(2, 1)
get(2)
set(3, 2)
get(2)
get(3)
Output:[1,-1,2]
Explanation:
cache cap is 1,set(2,1),get(2) and return 1,set(3,2) and delete (2,1),get(2) and return -1,get(3) and return 2.

解法1:

数据结构采用doubly linked list和hashmap。记得Node里面必须要加key这个字段,因为在删除元素(当capacity达到上限时)会用到mp.erase(head->next->key)。
注意LRUCache初始化时必须加上head和tail两个dummy node。
代码如下:

class Node {
public:
    Node *prev;
    Node *next;
    int key;
    int val;
    Node(int k = 0, int v = 0) : key(k), val(v), prev(NULL), next(NULL) {}
};

class LRUCache {
public:
    /*
    * @param capacity: An integer
    */
    LRUCache(int capacity) {
        this->capacity = capacity;
        this->head = new Node;  //dummy node
        this->tail = new Node;  //dummy node
        head->next = tail;
        tail->prev = head;
    }

    /*
     * @param key: An integer
     * @return: An integer
     */
    int get(int key) {
        if (mp.find(key) == mp.end()) return -1;
        Node *findNode = mp[key];
      //  findNode->prev->next = findNode->next;
      //    findNode->next->prev = findNode->prev;
        moveToTail(findNode);
        return findNode->val;
    }

    /*
     * @param key: An integer
     * @param value: An integer
     * @return: nothing
     */
    void set(int key, int value) {
        if (mp.find(key) != mp.end()) {
            mp[key]->val = value;
            moveToTail(mp[key]);
            return;
        } else if (mp.size() == capacity) { //LRU full, remove the least used node
            mp.erase(head->next->key);

            head->next = head->next->next;
            head->next->prev = head;
        }
        
        Node * node = new Node(key, value);
        mp[key] = node;
        moveToTail(node);
    }
    
private:
   Node *head, *tail;
   int capacity;
   map<int, Node *> mp;
   
   void moveToTail(Node * node) {
       if (node->prev) node->prev->next = node->next;
       if (node->next) node->next->prev = node->prev;
       
       node->prev = tail->prev;
       tail->prev->next = node;
       tail->prev = node;
       node->next = tail;
   }
};

代码同步在
https://github.com/luqian2017/Algorithm

二刷:

struct Node {
    int key;
    int val;
    Node *prev;
    Node *next;
    Node(int k, int v) : key(k), val(v), prev(nullptr), next(nullptr) {}
};

class LRUCache {
public:
    /*
    * @param capacity: An integer
    */
    LRUCache(int capacity) {
        cap = capacity;
        dummyHead = new Node(0, 0);
        dummyTail = new Node(0, 0);
        dummyHead->next = dummyTail;
        dummyTail->prev = dummyHead;
    }

    /*
     * @param key: An integer
     * @return: An integer
     */
    int get(int key) {
        if (keyMap.find(key) == keyMap.end()) {
            return -1;
        }
        move2Tail(key);
        return keyMap[key]->val;
    }

    /*
     * @param key: An integer
     * @param value: An integer
     * @return: nothing
     */
    void set(int key, int value) {
         if (keyMap.find(key) == keyMap.end()) {
            if (keyMap.size() == cap) {
                //delete least recently used
                keyMap.erase(dummyHead->next->key);
                dummyHead->next->next->prev = dummyHead;
                dummyHead->next = dummyHead->next->next;
            }
            keyMap[key] = new Node(key, value);
        } else {
            keyMap[key]->val = value;
        }
        move2Tail(key);
    }

private:
    int cap;
    unordered_map<int, Node *> keyMap; //<key, Node *>
    Node *dummyHead, *dummyTail;
    //head: least recently used 
    //tail: most recently used
    void move2Tail(int key) {
        Node *node = keyMap[key];
        //delete the key in doubly linked list
        if (node->prev != nullptr || node->next != nullptr) { //it is not a brand new node
           node->prev->next = node->next;
           node->next->prev = node->prev;
        }
           
        //insert to tail
        dummyTail->prev->next = node;
        node->prev = dummyTail->prev;
        node->next = dummyTail;
        dummyTail->prev = node;
    }

};

三刷:

struct Node {
    int key;
    int val;
    Node *prev, *next;
    Node(int k, int v) : key(k), val(v), prev(NULL), next(NULL) {}
};

class LRUCache {
public:
    LRUCache(int capacity) {
        front = new Node(-1, 0);
        tail = new Node(-1, 0);
        front->next = tail;
        tail->prev = front;
        cap = capacity;
    }
    
    int get(int key) {
        if (mp.find(key) == mp.end()) return -1;
        move2Tail(mp[key]);
        return mp[key]->val;
    }
    
    void put(int key, int value) {
        if (mp.find(key) != mp.end()) {
            mp[key]->val = value;
        } else {
            if (mp.size() == cap) {
                Node *tmp = front->next;
                front->next = tmp->next;
                front->next->prev = front;
                mp.erase(tmp->key);
                delete tmp;
            }
            Node *newNode = new Node(key, value);
            mp[key] = newNode;
        }        
        move2Tail(mp[key]);  
    }
private:
    map<int, Node *> mp; //<key, Node *>
    Node *front, *tail;
    int cap = 0;
    void move2Tail(Node * node) {
        Node *tailPrev = tail->prev;
        if (node != tailPrev) {
            if (node->prev) {
                Node *nodePrev = node->prev;
                nodePrev->next = node->next;
                node->next->prev = nodePrev;
            }
            tailPrev->next = node;
            node->prev = tailPrev;
            node->next = tail;
            tail->prev = node;
        }
    }
};

/**
 * 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);
 */
因果机器学习在农业决策中的应用:从预测到干预的范式转变 机器学习通过挖掘数据中的复杂模式,在预测任务上表现出色,但其核心关注的是变量间的相关关系。然而,在农业等复杂系统中,理解变量间的因果关系对于制定有效干预措施至关重要。因果推断提供了从“关联”到“因果”的数学框架,旨在揭示干预如何导致结果变化。因果机器学习融合了二者的优势,利用机器学习强大的数据处理能力服务于因果效应的精准估计,同时借助因果思维提升模型的稳健性和可解释性。这一技术价值在于,它能回答“如果采取某项措施,结果会怎样”的决策核心问题,从而支持精准农业、政策评估和个性化农艺推荐。在农业领域,这具体应用 阅读详情

相关推荐

Bioinformatics2021 | FraGAT+:基于分子原始片段的多尺度图注意力机制的分子性质预测模型

Bioinformatics2021 | FraGAT:基于分子原始片段的多尺度图注意力机制的分子性质预测模型

justBeHerHero 2003

C++常用语法

unordered_map<int, Node*> mp; if (mp.find(key) == mp.end()) unordered_map<int, Node*>::iterator it = mp.find(deltmp->key); mp.erase(it); mp.clear(); map[k]++ // this include...

weixin_30304375的博客 89

告别外置天线!手把手教你用HFSS仿真设计一个20x40mm的433MHz PCB蛇形天线

本文详细介绍了如何使用HFSS仿真工具设计一个20x40mm的433MHz PCB蛇形天线,解决物联网设备小型化中的天线集成难题。通过对比外置天线与PCB天线的性能差异,深入解析蛇形天线的电磁学原理,并提供从建模到优化的全流程实战指南,帮助工程师实现高效、低成本的微型化天线设计。

weixin_30606461的博客 434

LRU Cache 题解

题意 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if ...

weixin_30312557的博客 152

PaddleOCR 与 PaddleX 调试

本文介绍了PaddleOCR与PaddleX的安装调试过程及应用对比。首先详细说明了环境准备步骤,包括安装ccache、设置环境变量及创建conda虚拟环境,提供了精简安装和源码安装两种方式。接着展示了使用PaddleOCR进行发票和手写汉字识别的测试结果,指出部分手写识别存在误差。随后对比了两者的差异:PaddleOCR专注于OCR算法优化,PaddleX则提供全流程工具链和标准化部署方案。最后演示了如何通过PaddleX部署OCR服务,包括API启动方法和客户端调用示例,为开发者提供了两种不同场景下的O

weixin_42176639的博客 1452

基于 SpringAI 整合 DeepSeek 模型实现 AI 聊天对话

很多云平台都提供了一键部署大模型的功能,这里不再赘述(阿里云百炼平台、火山方舟-火山引擎等)Ollama。

📜一个热爱探索的人 1280

LintCode算法题:LRU Cache

什么是LRU 百度和谷歌有相关的连接介绍,本文不赘述。 LRU_百度百科 题目 链接:LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) ...

agaghd的博客 289

ComfyUI+minimaxh3本地部署:漫剧分镜批量生成工作流全实操

视频生成模型正从云端API向本地化部署迁移,背后的技术逻辑是将扩散模型的文本编码、潜空间采样、VAE解码等阶段拆分为可编排的独立环节。ComfyUI作为节点化管线工具,恰好能以可视化连线方式管理这些阶段,让生成过程可控、可复用、可批量执行。minimaxh3模型支持本地推理,配合LoRA微调与参数优化,能够在保证角色一致性的前提下,从分镜脚本批量产出连续剧情片段,规避云端排队、成本高、素材不可控等工程问题。这套工作流广泛应用于漫剧、短剧和动态条漫的工业化生产,尤其适合需要高频迭代分镜草稿的内容团队。文本以零

weixin_29057065的博客 371

LintCode_134 LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if

Bernini @ buffalo 425

Verilator+VSCode实战:5分钟搞定Verilog语法检查与波形调试

本文详细介绍了如何将Verilator仿真器与VSCode编辑器高效集成,打造极速的Verilog开发工作流。通过环境配置、实时语法检查、一键仿真与波形生成等实战技巧,帮助IC设计工程师在几分钟内完成从代码编写到波形调试的全过程,大幅提升中小规模模块的验证效率。

weixin_28454475的博客 471

手把手教你学Simulink——基于多能互补微电网系统的建模与优化场景实例:风柴储联合供电系统在孤岛模式下的功率分配建模

本文介绍了使用MATLAB/Simulink和Simscape Electrical构建风柴储联合供电系统的建模方法。系统包含风力发电、柴油发电、储能电池等模块,重点研究孤岛模式下功率分配策略。详细说明了从创建项目、添加模块到搭建风力发电系统(含MPPT控制)、柴油发电系统及集成各模块的全过程,并提供了仿真参数设置建议。通过分析频率偏差、电池SOC等关键指标,验证了系统在维持稳定运行的同时能有效利用可再生能源。该模型为研究多能互补微电网提供了实验平台,未来可拓展更高效的能量管理策略和AI技术的应用。

MHD0815的博客 1万+

lintcode练习- 134. LRU缓存策略

134. LRU缓存策略 为最近最少使用(LRU)缓存策略设计一个数据结构,它应该支持以下操作:获取数据(get)和写入数据(set)。 获取数据get(key):如果缓存中存在key,则获取其数据值(通常是正数),否则返回-1。 写入数据set(key, value):如果key还没有在缓存中,则写入其数据值。当缓存达到上限,它应该在写入新数据之前删除最近最少使用的数据用来腾出空闲位置。 ...

云中寻雾的博客 597

Hermes Agent 彩蛋大盘点:藏在终端里的浪漫

本文主要介绍了Hermes Agent 彩蛋大盘点:藏在终端里的浪漫,希望能对使用Hermes Agent的同学们有所帮助。 文章目录 1. 前言 2. Spinner 动画 3. Kawaii 表情 4. 思考动词 THINKING_VERBS 5. 皮肤主题 6. 启动 Banner 7. 工具 Emoji 8. 内置人格 9. SOUL.md —— Agent 的灵魂 10. BOOT.md —— 启动自动执行 11. 随机 Tips 12. 统计总览

weixin_43178406的博客 7万+

LRU Cache -- Lintcode 134

原题连接: http://www.lintcode.com/en/problem/lru-cache/ Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key)

gunman_lb的博客 368

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

【Lintcode】134. LRU Cache

题目地址: https://www.lintcode.com/problem/lru-cache/description 设计一个数据结构作为Least Recently Used Cache。它可以实现下面的操作: 1、get(key),得到key对应的value(题目保证得到的value一定是正数)。如果对应的key不存在,则返回−1-1−1; 2、set(key, value),如果cache里不存在这样的key,就添加进去;否则就将key对应的value设成参数value; 3、cache有容量限制

数学、算法爱好者的博客 177

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Lintcode134 Lru Cache solution 题解

【题目描述】 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset. get(key)- Get the value (will always be positive) of the k

nekomilk的博客 355

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

攻克LintCode Hard难题:20道最烧脑题目完整清单与突破指南

LintCode C++11 Solutions of All 289 LintCode Problems 仓库收录了 LintCode 平台全部 289 道题目的 C++11 题解。本文从中精选 20 道 Hard 难度题目,给出完整清单与突破方法,帮你快速定位高频考点、按顺序通关 🎯 --- ## Hard 难度题目筛选:从 48 道到 20 道 整个仓库共覆盖 289 道题,其中标注

gitblog_01033的博客 243

Lintcode 134. LRU Cache (Medium) (Python)

LRU Cache Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be posi...

DY的博客 314

LintCode:134.LRU缓存策略

描述为最近最少使用(LRU)缓存策略设计一个数据结构,它应该支持以下操作:获取数据(get)和写入数据(set)。获取数据get(key):如果缓存中存在key,则获取其数据值(通常是正数),否则返回-1。写入数据set(key, value):如果key还没有在缓存中,则写入其数据值。当缓存达到上限,它应该在写入新数据之前删除最近最少使用的数据用来腾出空闲位置。分析可以定义一个双向链表来实现元素...

HxxxxxxU的博客 507

剑指offer/LeetCode146/LintCode134_LRU缓存实现

剑指offer/LeetCode146/LintCode134_LRU缓存实现 声明 文章均为本人技术笔记,转载请注明出处[1] https://segmentfault.com/u/yzwall[2] blog.youkuaiyun.com/j_dark/ 解题思路 LRU缓存两种功能: get(key):获取key的对应value,不存在返回-...

weixin_33828101的博客 282

python--lintcode134. LRU缓存策略

描述为最近最少使用(LRU)缓存策略设计一个数据结构,它应该支持以下操作:获取数据(get)和写入数据(set)。获取数据get(key):如果缓存中存在key,则获取其数据值(通常是正数),否则返回-1。写入数据set(key, value):如果key还没有在缓存中,则写入其数据值。当缓存达到上限,它应该在写入新数据之前删除最近最少使用的数据用来腾出空闲位置。哇困难级别的一题,相当有意思。我举...

wenqiwenqi123的博客 583

【哈希表与堆】Lintcode 134. LRU缓冲策略

Lintcode 134. LRU缓冲策略 题目描述:为最近最少使用(LRU)缓存策略设计一个数据结构,它应该支持以下操作:获取数据和写入数据。 get(key) 获取数据:如果缓存中存在key,则获取其数据值(通常是正数),否则返回-1。 set(key, value) 写入数据:如果key还没有在缓存中,则写入其数据值。当缓存达到上限,它应该在写入新数据之前删除最近最少使用的数据用来腾出空闲位置。 最终, 你需要返回每次 get 的数据。 代码使用哈希表+单向链表实现: class KeyVal

享受当下 253

【三次过】Lintcode 134. LRU缓存策略

为最近最少使用(LRU)缓存策略设计一个数据结构,它应该支持以下操作:获取数据(get)和写入数据(set)。 获取数据get(key):如果缓存中存在key,则获取其数据值(通常是正数),否则返回-1。 写入数据set(key, value):如果key还没有在缓存中,则写入其数据值。当缓存达到上限,它应该在写入新数据之前删除最近最少使用的数据用来腾出空闲位置。 样例 样例1 输入:...

majichen95的博客 374

lintcode解题笔记:Reverse Pairs

532.Reverse Pairs For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair. return total of reverse pairs in A. Example: Given A = [2, 4, 1, 3, 5] , (2, 1), (4,

(づ。◕‿‿◕。)づgigi奋斗在前端和刷题中╮(╯﹏╰)╭ 2022

LintCode-33: N-Queens (DFS排列经典题!)

又是一道DFS排列经典题。这题用visited[]还不够,要专门用一个isValid()函数来确保没有列冲突和两个对角线冲突。 注意: 1) string的初始化: string dotStr(n, ‘.’); vector solString(n, dotStr); //good string initialization code! ...

roufoo的博客 408

LeetCode上一道经典的面试题-O(1)实现LRU Cache

参考这个博客这里写链接内容 思路: 每次数据项被查询到时,都将此数据项移动到链表头部(O(1)的时间复杂度) 这样,在进行过多次查找操作后,最近被使用过的内容就向链表的头移动,而没有被使用的内容就向链表的后面移动。 当需要替换时,链表最后的位置就是最近最少被使用的数据项,我们只需要将最新的数据项放在链表头部,当Cache满时,淘汰链表最后的位置就是了。 还有这个博客简单的LRU Ca...

JackZhangNJU的专栏 896
上一篇: LintCode 4: Ugly Number II (重要)
下一篇: LintCode 192: Wildcard Matching (经典题)
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值