1. 首页

LeetCode 146. LRU缓存机制

题目描述

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(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.

Follow up:
Could you do both operations in O(1) time complexity?

Example:

LRUCache cache = new LRUCache( 2 /* capacity */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // returns 1
cache.put(3, 3);    // evicts key 2
cache.get(2);       // returns -1 (not found)
cache.put(4, 4);    // evicts key 1
cache.get(1);       // returns -1 (not found)
cache.get(3);       // returns 3
cache.get(4);       // returns 4

难度:Middle

前置知识

  • 队列

公司

  • 阿里
  • 腾讯
  • 百度
  • 字节

思路

本题已被收录到我的新书中,敬请期待~

由于是保留是最近使用的 N 条数据,这就和队列的特性很符合, 先进入队列的,先出队列。

因此思路就是用一个队列来记录目前缓存的所有 key, 每次 push 都进行判断,如果
超出最大容量限制则进行清除缓存的操作, 具体清除谁就按照刚才说的队列方式进行处理,同时对 key 进行入队操作。

get 的时候,如果缓存中有,则调整队列(具体操作为删除指定元素和入队两个操作)。 缓存中没有则返回-1

关键点解析

  • 队列简化操作

  • 队列的操作是这道题的灵魂, 很容易少考虑情况

代码

/**
 * @来源: Javascript中文网 - 前端进阶资源教程 https://www.javascriptc.com/
 * @介绍:是以前端进阶资源教程分享为主的专业网站,包括:前端、大厂面试题、typescript教程、程序人生、React.js
 * @param {number} capacity
 */
var LRUCache = function(capacity) {
    this.cache = {};
    this.capacity = capacity;
    this.size = 0;
    this.queue = [];
};

/**
 * @param {number} key
 * @return {number}
 */
LRUCache.prototype.get = function(key) {
    const hit = this.cache[key];

    if (hit !== undefined) {
        this.queue = this.queue.filter(q => q !== key);
        this.queue.push(key);
        return hit;
    }
    return -1;
};

/**
 * @param {number} key
 * @param {number} value
 * @return {void}
 */
LRUCache.prototype.put = function(key, value) {
    const hit = this.cache[key];

    // update cache
    this.cache[key] = value;

    if (!hit) {
        // invalid cache and resize size;
        if (this.size === this.capacity) {
            // invalid cache
            const key = this.queue.shift();
            this.cache[key] = undefined;
        } else {
            this.size = this.size + 1;
        }
        this.queue.push(key);
    } else {
        this.queue = this.queue.filter(q => q !== key);
        this.queue.push(key);
    }
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * var obj = new LRUCache(capacity)
 * var param_1 = obj.get(key)
 * obj.put(key,value)
 */

本题删除的时间复杂度是不符合要求的。 应该采用双向链表在 O(1) 时间找到前驱进行删除。

更多题解可以访问我的 JS中文网 – 码农周刊 仓库:https://github.com/meibin08/free-programming-books, 一起学习更多前端前沿知识。

看完两件小事

如果你觉得这篇文章对你挺有启发,我想请你帮我两个小忙:

  1. 关注我们的 GitHub 博客,让我们成为长期关系
  2. 把这篇文章分享给你的朋友 / 交流群,让更多的人看到,一起进步,一起成长!
  3. 关注公众号 「画漫画的程序员」,公众号后台回复「资源」 免费领取我精心整理的前端进阶资源教程

JS中文网是中国领先的新一代开发者社区和专业的技术媒体,一个帮助开发者成长的社区,目前已经覆盖和服务了超过 300 万开发者,你每天都可以在这里找到技术世界的头条内容。欢迎热爱技术的你一起加入交流与学习,JS中文网的使命是帮助开发者用代码改变世界

本文著作权归作者所有,如若转载,请注明出处

转载请注明:文章转载自「 Js中文网 · 前端进阶资源教程 」https://www.javascriptc.com

标题:LeetCode 146. LRU缓存机制

链接:https://www.javascriptc.com/4510.html

« react-intersection-observer 源码
LeetCode 145. 二叉树的后序遍历»
Flutter 中文教程资源

相关推荐

QR code